gVirtualXRay  2.0.10
VirtualX-RayImagingLibraryonGPU
Context.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2013-2023, Dr Franck P. Vidal, Bangor University, All rights reserved.
4 Copyright (c) 2023-present, Prof Franck P. Vidal (franck.vidal@stfc.ac.uk),
5 UK Research and Innovation, All rights reserved.
6 
7 
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice,
12 this list of conditions and the following disclaimer.
13 
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation and/or
16 other materials provided with the distribution.
17 
18 3. Neither the name of Bangor University, UK Research and Innovation nor the
19 names of its contributors may be used to endorse or promote products derived
20 from this software without specific prior written permission.
21 
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 */
34 
35 
63 #ifndef __Context_h
64 #define __Context_h
65 
66 
67 //******************************************************************************
68 // defines
69 //******************************************************************************
70 #ifndef EGL_NO_X11
71 #define EGL_NO_X11
72 #endif
73 
74 #ifndef MESA_EGL_NO_X11_HEADERS
75 #define MESA_EGL_NO_X11_HEADERS
76 #endif
77 
78 
79 //******************************************************************************
80 // Include
81 //******************************************************************************
82 #include <string>
83 
84 #include <vector>
85 
86 #ifdef HAS_EGL
87 #include <EGL/egl.h>
88 #include <EGL/eglext.h>
89 #endif
90 
91 #ifdef HAS_GLX
92 #include <GL/glx.h>
93 #endif
94 
95 #define GLFW_INCLUDE_GLCOREARB 1
96 #include <GLFW/glfw3.h>
97 
98 
99 //******************************************************************************
100 // namespace
101 //******************************************************************************
102 namespace gVirtualXRay {
103 
104 
105 //==============================================================================
110 //==============================================================================
111 class Context
112 //------------------------------------------------------------------------------
113 {
114 //******************************************************************************
115 public:
116  //--------------------------------------------------------------------------
118  //--------------------------------------------------------------------------
119  Context();
120 
121 
122  //--------------------------------------------------------------------------
124  /*
125  * @param aRenderer: the type of context. Accepted values are "OpenGL", "Vulkan", and "EGL" (default: "OpenGL")
126  * @param aRendererMajorVersion: Select the major version of the renderer.
127  * (default value: 3)
128  * @param aRendererMajorVersion: Select the minor version of the renderer.
129  * (default value: 2)
130  */
131  //--------------------------------------------------------------------------
132  Context(const std::string& aRenderer,
133  int aRendererMajorVersion,
134  int aRendererMinorVersion);
135 
136 
137  //--------------------------------------------------------------------------
139  //--------------------------------------------------------------------------
140  ~Context();
141 
142 
143  //--------------------------------------------------------------------------
145  /*
146  * @param aRenderer: the type of context. Accepted values are "OpenGL", "Vulkan", and "EGL" (default: "OpenGL")
147  * @param aRendererMajorVersion: Select the major version of the renderer.
148  * (default value: 3)
149  * @param aRendererMajorVersion: Select the minor version of the renderer.
150  * (default value: 2)
151  */
152  //--------------------------------------------------------------------------
153  void create(const std::string& aRenderer = "OpenGL",
154  int aRendererMajorVersion = 3,
155  int aRendererMinorVersion = 2);
156 
157 
158  //--------------------------------------------------------------------------
160  //--------------------------------------------------------------------------
161  void release();
162 
163 
164  //--------------------------------------------------------------------------
166  //--------------------------------------------------------------------------
167  void destroy();
168 
169 
170  bool isReady() const;
171 
172 
173  GLFWwindow* getWindowHandle();
174  const GLFWwindow* getWindowHandle() const;
175 
176 
177  //--------------------------------------------------------------------------
179  /*
180  * @return on the number of EGL devices
181  */
182  //--------------------------------------------------------------------------
183  static unsigned int getNumberOfEGLDevices();
184 
185 
186  //--------------------------------------------------------------------------
188  /*
189  * @return on the list of supported EGL clients
190  */
191  //--------------------------------------------------------------------------
192  static std::vector<std::string> findEGLClients();
193 
194 
195 //******************************************************************************
196 protected:
197  //--------------------------------------------------------------------------
199  //--------------------------------------------------------------------------
200  void createGLWithGLX();
201 
202 
203  //--------------------------------------------------------------------------
205  //--------------------------------------------------------------------------
206  void createGLWithEGL();
207 
208 
209  //--------------------------------------------------------------------------
211  /*
212  * @param aVisibilityFlag: flag controling if the window should be visible (1)
213  * or hidden (0). (default value: 0)
214  */
215  //--------------------------------------------------------------------------
216  void createGLWithGLFW(int aVisibilityFlag = 0);
217 
218 
219  //--------------------------------------------------------------------------
221  /*
222  * @param aVisibilityFlag: flag controling if the window should be visible (1)
223  * or hidden (0). (default value: 0)
224  */
225  //--------------------------------------------------------------------------
226  void createVulkanWithGLFW(int aVisibilityFlag = 0);
227 
228 
229 #ifdef HAS_EGL
230  static const char* getDisplayExtensions(EGLDisplay aDisplay);
231  void createDisplay(int aPlatform, const std::string& aPlatformStr, int aDeviceID = -1);
232  void createEGLContextFromDisplay();
233  bool initialiseEGL(std::vector<std::string>& anEGLClientSet);
234 #endif
235 
236 #ifdef HAS_VULKAN
237  struct {
238  VkPhysicalDevice physical_device;
239  VkDevice logical_device;
240  } m_vk_main_device;
241 
242  // Indices (locations) of Queue Families (if they exist at all)
243  struct QueueFamilyIndices {
244  int graphics_family = -1; // Location of Graphics Queue Family
245 
246  // Check if queue families are valid
247  bool isValid()
248  {
249  return graphics_family >= 0;
250  }
251  };
252 
253  bool createVkInstance();
254  bool createVkLogicalDevice();
255  bool getVkPhysicalDevice();
256  bool checkVkInstanceExtensionSupport(std::vector<const char*>* checkExtensions);
257 
258  QueueFamilyIndices getQueueFamilies(VkPhysicalDevice device);
259 #endif
260 
261 
262  std::string m_renderer;
265 
266  GLFWwindow* m_p_window;
267 
268 #ifdef HAS_EGL
269  EGLDisplay m_display;
270  EGLConfig m_config;
271  EGLContext m_context;
272  EGLSurface m_surface;
273  std::vector<EGLDeviceEXT> m_p_egl_devices_set;
274  int m_device_id;
275 #endif
276 
277 #ifdef HAS_GLX
278  Display* m_glx_display;
279  Colormap m_glx_cmap;
280  GLXContext m_glx_context;
281  Window m_glx_window;
282 #endif
283 
284 #ifdef HAS_VULKAN
285  VkInstance m_vk_instance;
286 
287  VkQueue m_vk_graphics_queue;
288 #endif
289 
290  bool m_ready;
291 };
292 
293 
294 } // namespace gVirtualXRay
295 
296 
297 
298 #endif
std::string m_renderer
Definition: Context.h:262
void destroy()
Destroy the context.
void createVulkanWithGLFW(int aVisibilityFlag=0)
Create a Vulkan context using GLFW.
void createGLWithGLFW(int aVisibilityFlag=0)
Create an OpenGL context using GLFW.
Context()
Default Constructor.
bool isReady() const
static unsigned int getNumberOfEGLDevices()
Accessor on the number of EGL devices.
GLFWwindow * getWindowHandle()
void create(const std::string &aRenderer="OpenGL", int aRendererMajorVersion=3, int aRendererMinorVersion=2)
Create a new context.
GLFWwindow * m_p_window
Definition: Context.h:266
void release()
Destroy the context.
void createGLWithGLX()
Create an OpenGL context using GLX.
Context is a class to manage an OpenGL or Vulkan context.
Definition: Context.h:111
void createGLWithEGL()
Create an OpenGL context using EGL.
~Context()
Destructor.
static std::vector< std::string > findEGLClients()
Accessor on the list of supported EGL clients.