File: vtkOpenXRManagerOpenGLGraphics.cxx

package info (click to toggle)
paraview 5.11.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 497,236 kB
  • sloc: cpp: 3,171,290; ansic: 1,315,072; python: 134,290; xml: 103,324; sql: 65,887; sh: 5,286; javascript: 4,901; yacc: 4,383; java: 3,977; perl: 2,363; lex: 1,909; f90: 1,255; objc: 143; makefile: 119; tcl: 59; pascal: 50; fortran: 29
file content (219 lines) | stat: -rw-r--r-- 8,452 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkOpenXRManagerOpenGLGraphics.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "vtkOpenXRManagerOpenGLGraphics.h"

#include "vtkObjectFactory.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenXRManager.h"

#define XR_USE_GRAPHICS_API_OPENGL
#include <vtkOpenXRPlatform.h>

// include what we need for the helper window
#ifdef VTK_USE_X
// We need to cast to a XOpenGLRenderWindow for vtkXVisualInfo->visualid
#include "vtkXOpenGLRenderWindow.h"

// From vtkXOpenGLRenderWindow.cxx :
// Work-around to get forward declarations of C typedef of anonymous
// structs working. We do not want to include XUtil.h in the header as
// it populates the global namespace.
#include "GL/glx.h"
#include <X11/Xutil.h>
VTK_ABI_NAMESPACE_BEGIN
struct vtkXVisualInfo : public XVisualInfo
{
};
VTK_ABI_NAMESPACE_END
#endif // VTK_USE_X

VTK_ABI_NAMESPACE_BEGIN
class vtkOpenXRManagerOpenGLGraphics::PIMPL
{
public:
  // OpenGL swapchains
  std::vector<SwapchainImagesOpenGL> ColorSwapchains;
  std::vector<SwapchainImagesOpenGL> DepthSwapchains;
};

vtkStandardNewMacro(vtkOpenXRManagerOpenGLGraphics);

//------------------------------------------------------------------------------
vtkOpenXRManagerOpenGLGraphics::vtkOpenXRManagerOpenGLGraphics()
{
  this->Private = new PIMPL;
}

//------------------------------------------------------------------------------
vtkOpenXRManagerOpenGLGraphics::~vtkOpenXRManagerOpenGLGraphics()
{
  delete this->Private;
}

//------------------------------------------------------------------------------
void vtkOpenXRManagerOpenGLGraphics::SetNumberOfSwapchains(uint32_t viewCount)
{
  this->Private->ColorSwapchains.resize(viewCount);
  this->Private->DepthSwapchains.resize(viewCount);
};

//------------------------------------------------------------------------------
void vtkOpenXRManagerOpenGLGraphics::GetColorSwapchainImage(
  uint32_t scIndex, uint32_t imgIndex, void* texture)
{
  *(GLuint*)texture = this->Private->ColorSwapchains[scIndex].Images[imgIndex].image;
}

//------------------------------------------------------------------------------
void vtkOpenXRManagerOpenGLGraphics::GetDepthSwapchainImage(
  uint32_t scIndex, uint32_t imgIndex, void* texture)
{
  *(GLuint*)texture = this->Private->DepthSwapchains[scIndex].Images[imgIndex].image;
}

//------------------------------------------------------------------------------
void vtkOpenXRManagerOpenGLGraphics::EnumerateColorSwapchainImages(
  XrSwapchain swapchain, uint32_t scIndex)
{
  this->EnumerateSwapchainImages(swapchain, this->Private->ColorSwapchains[scIndex]);
}

//------------------------------------------------------------------------------
void vtkOpenXRManagerOpenGLGraphics::EnumerateDepthSwapchainImages(
  XrSwapchain swapchain, uint32_t scIndex)
{
  this->EnumerateSwapchainImages(swapchain, this->Private->DepthSwapchains[scIndex]);
}

//------------------------------------------------------------------------------
const std::vector<int64_t>& vtkOpenXRManagerOpenGLGraphics::GetSupportedColorFormats()
{
  const static std::vector<int64_t> supportedColorFormats = { GL_RGBA32F, GL_RGBA16F, GL_RGBA16,
    GL_SRGB8_ALPHA8_EXT };
  return supportedColorFormats;
}

//------------------------------------------------------------------------------
const std::vector<int64_t>& vtkOpenXRManagerOpenGLGraphics::GetSupportedDepthFormats()
{
  const static std::vector<int64_t> supportedDepthFormats = { GL_DEPTH_COMPONENT16,
    GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT32F };
  return supportedDepthFormats;
}

//------------------------------------------------------------------------------
void vtkOpenXRManagerOpenGLGraphics::EnumerateSwapchainImages(
  XrSwapchain swapchain, SwapchainImagesOpenGL& swapchainImages)
{
  uint32_t chainLength = this->GetChainLength(swapchain);

  swapchainImages.Images.resize(chainLength, { XR_TYPE_SWAPCHAIN_IMAGE_OPENGL_KHR });

  vtkOpenXRManager::GetInstance().XrCheckError(
    xrEnumerateSwapchainImages(swapchain, (uint32_t)swapchainImages.Images.size(), &chainLength,
      reinterpret_cast<XrSwapchainImageBaseHeader*>(swapchainImages.Images.data())),
    "Failed to enumerate swapchain images");
}

//------------------------------------------------------------------------------
bool vtkOpenXRManagerOpenGLGraphics::CreateGraphicsBinding(vtkOpenGLRenderWindow* helperWindow)
{
#ifdef VTK_USE_X
  // Create the XrGraphicsBindingOpenGLXlibKHR structure
  // That will be in the next chain of the XrSessionCreateInfo
  // We need to fill xDisplay, visualId, glxFBConfig, glxDrawable and glxContext

  auto graphicsBindingGLX =
    std::shared_ptr<XrGraphicsBindingOpenGLXlibKHR>(new XrGraphicsBindingOpenGLXlibKHR{
      XR_TYPE_GRAPHICS_BINDING_OPENGL_XLIB_KHR, // .type
      nullptr,                                  // .next
      nullptr,                                  // .xDisplay. a valid X11 display
      0,                                        // .visualid. a valid X11 visual id
      0,                                        // .glxFBConfig. a valid X11 OpenGL GLX GLXFBConfig
      0,                                        // .glxDrawable. a valid X11 OpenGL GLX GLXDrawable
      0                                         // .glxContext. a valid X11 OpenGL GLX GLXContext
    });
  this->GraphicsBinding = graphicsBindingGLX;

  vtkNew<vtkXOpenGLRenderWindow> xoglRenWin;
  vtkXOpenGLRenderWindow* glxHelperWindow = vtkXOpenGLRenderWindow::SafeDownCast(helperWindow);

  if (glxHelperWindow == nullptr)
  {
    xoglRenWin->InitializeFromCurrentContext();
    glxHelperWindow = xoglRenWin;
  }

  vtkXVisualInfo* v = glxHelperWindow->GetDesiredVisualInfo();
  GLXFBConfig* fbConfig = reinterpret_cast<GLXFBConfig*>(glxHelperWindow->GetGenericFBConfig());

  graphicsBindingGLX->xDisplay = glxHelperWindow->GetDisplayId();
  graphicsBindingGLX->glxDrawable = glxHelperWindow->GetWindowId();
  graphicsBindingGLX->glxContext = glXGetCurrentContext();
  graphicsBindingGLX->visualid = v->visualid;
  graphicsBindingGLX->glxFBConfig = *fbConfig;

#elif defined(_WIN32)
  (void)helperWindow; // Hide unused parameter warning
  auto graphicsBindingGLWin32 =
    std::shared_ptr<XrGraphicsBindingOpenGLWin32KHR>(new XrGraphicsBindingOpenGLWin32KHR{
      XR_TYPE_GRAPHICS_BINDING_OPENGL_WIN32_KHR, // .type
      nullptr,                                   // .next
      0,                                         // .hdC : a valid Windows HW device context handle.
      0 // .hGLRChandle : a valid Windows OpenGL rendering context
    });
  this->GraphicsBinding = graphicsBindingGLWin32;

  graphicsBindingGLWin32->hDC = wglGetCurrentDC();
  graphicsBindingGLWin32->hGLRC = wglGetCurrentContext();

#else
  (void)helperWindow; // Hide unused parameter warning
  vtkErrorMacro(<< "Only X11 and Win32 are supported at the moment.");
  return false;
#endif

  return true;
}

//------------------------------------------------------------------------------
bool vtkOpenXRManagerOpenGLGraphics::CheckGraphicsRequirements(XrInstance instance, XrSystemId id)
{
  XrGraphicsRequirementsOpenGLKHR openGLReqs = {
    XR_TYPE_GRAPHICS_REQUIREMENTS_OPENGL_KHR, // .type
    nullptr,                                  // .next
    0,                                        // .minApiVersionSupported
    0                                         // .maxApiVersionSupported
  };

  xr::GraphicsExtensionDispatchTable extensions;
  extensions.PopulateDispatchTable(instance);

  if (!vtkOpenXRManager::GetInstance().XrCheckError(
        extensions.xrGetOpenGLGraphicsRequirementsKHR(instance, id, &openGLReqs),
        "Failed to get OpenGL graphics requirements!"))
  {
    return false;
  }

  return true;
}

//------------------------------------------------------------------------------
const char* vtkOpenXRManagerOpenGLGraphics::GetBackendExtensionName()
{
  return XR_KHR_OPENGL_ENABLE_EXTENSION_NAME;
}
VTK_ABI_NAMESPACE_END