Check runtime OpenGL requirements prior to creating graphics binding
[blender.git] / intern / ghost / intern / GHOST_XrGraphicsBinding.cpp
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16
17 /** \file
18  * \ingroup GHOST
19  */
20
21 #include <algorithm>
22 #include <list>
23 #include <sstream>
24
25 #if defined(WITH_X11)
26 #  include "GHOST_ContextGLX.h"
27 #elif defined(WIN32)
28 #  include "GHOST_ContextWGL.h"
29 #  include "GHOST_ContextD3D.h"
30 #endif
31 #include "GHOST_C-api.h"
32 #include "GHOST_Xr_intern.h"
33
34 #include "GHOST_IXrGraphicsBinding.h"
35
36 static bool choose_swapchain_format_from_candidates(std::vector<int64_t> gpu_binding_formats,
37                                                     std::vector<int64_t> runtime_formats,
38                                                     int64_t *r_result)
39 {
40   if (gpu_binding_formats.empty()) {
41     return false;
42   }
43
44   auto res = std::find_first_of(gpu_binding_formats.begin(),
45                                 gpu_binding_formats.end(),
46                                 runtime_formats.begin(),
47                                 runtime_formats.end());
48   if (res == gpu_binding_formats.end()) {
49     return false;
50   }
51
52   *r_result = *res;
53   return true;
54 }
55
56 class GHOST_XrGraphicsBindingOpenGL : public GHOST_IXrGraphicsBinding {
57  public:
58   bool checkVersionRequirements(GHOST_Context *ghost_ctx,
59                                 XrInstance instance,
60                                 XrSystemId system_id,
61                                 std::string *r_requirement_info) const override
62   {
63 #if defined(WITH_X11)
64     GHOST_ContextGLX *ctx_gl = static_cast<GHOST_ContextGLX *>(ghost_ctx);
65 #else
66     GHOST_ContextWGL *ctx_gl = static_cast<GHOST_ContextWGL *>(ghost_ctx);
67 #endif
68     XrGraphicsRequirementsOpenGLKHR gpu_requirements{XR_TYPE_GRAPHICS_REQUIREMENTS_OPENGL_KHR};
69     const uint32_t gl_version = XR_MAKE_VERSION(
70         ctx_gl->m_contextMajorVersion, ctx_gl->m_contextMinorVersion, 0);
71
72     xrGetOpenGLGraphicsRequirementsKHR(instance, system_id, &gpu_requirements);
73
74     if (r_requirement_info) {
75       std::ostringstream strstream;
76       strstream << "Min OpenGL version "
77                 << XR_VERSION_MAJOR(gpu_requirements.minApiVersionSupported) << "."
78                 << XR_VERSION_MINOR(gpu_requirements.minApiVersionSupported) << std::endl;
79       strstream << "Max OpenGL version "
80                 << XR_VERSION_MAJOR(gpu_requirements.maxApiVersionSupported) << "."
81                 << XR_VERSION_MINOR(gpu_requirements.maxApiVersionSupported) << std::endl;
82
83       *r_requirement_info = std::move(strstream.str());
84     }
85
86     return (gl_version >= gpu_requirements.minApiVersionSupported) &&
87            (gl_version <= gpu_requirements.maxApiVersionSupported);
88   }
89
90   void initFromGhostContext(GHOST_Context *ghost_ctx) override
91   {
92 #if defined(WITH_X11)
93     GHOST_ContextGLX *ctx_glx = static_cast<GHOST_ContextGLX *>(ghost_ctx);
94     XVisualInfo *visual_info = glXGetVisualFromFBConfig(ctx_glx->m_display, ctx_glx->m_fbconfig);
95
96     oxr_binding.glx.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_XLIB_KHR;
97     oxr_binding.glx.xDisplay = ctx_glx->m_display;
98     oxr_binding.glx.glxFBConfig = ctx_glx->m_fbconfig;
99     oxr_binding.glx.glxDrawable = ctx_glx->m_window;
100     oxr_binding.glx.glxContext = ctx_glx->m_context;
101     oxr_binding.glx.visualid = visual_info->visualid;
102 #elif defined(WIN32)
103     GHOST_ContextWGL *ctx_wgl = static_cast<GHOST_ContextWGL *>(ghost_ctx);
104
105     oxr_binding.wgl.type = XR_TYPE_GRAPHICS_BINDING_OPENGL_WIN32_KHR;
106     oxr_binding.wgl.hDC = ctx_wgl->m_hDC;
107     oxr_binding.wgl.hGLRC = ctx_wgl->m_hGLRC;
108 #endif
109   }
110
111   bool chooseSwapchainFormat(const std::vector<int64_t> &runtime_formats,
112                              int64_t *r_result) const override
113   {
114     std::vector<int64_t> gpu_binding_formats = {GL_RGBA8};
115     return choose_swapchain_format_from_candidates(gpu_binding_formats, runtime_formats, r_result);
116   }
117
118   std::vector<XrSwapchainImageBaseHeader *> createSwapchainImages(uint32_t image_count) override
119   {
120     std::vector<XrSwapchainImageOpenGLKHR> ogl_images(image_count);
121     std::vector<XrSwapchainImageBaseHeader *> base_images;
122
123     // Need to return vector of base header pointers, so of a different type. Need to build a new
124     // list with this type, and keep the initial one alive.
125     for (XrSwapchainImageOpenGLKHR &image : ogl_images) {
126       image.type = XR_TYPE_SWAPCHAIN_IMAGE_OPENGL_KHR;
127       base_images.push_back(reinterpret_cast<XrSwapchainImageBaseHeader *>(&image));
128     }
129
130     // Keep alive.
131     m_image_cache.push_back(std::move(ogl_images));
132
133     return base_images;
134   }
135
136   void drawViewBegin(XrSwapchainImageBaseHeader *swapchain_image) override
137   {
138     // TODO
139     (void)swapchain_image;
140   }
141   void drawViewEnd(XrSwapchainImageBaseHeader *swapchain_image, GHOST_Context *ogl_ctx) override
142   {
143     // TODO
144     (void)swapchain_image;
145     (void)ogl_ctx;
146   }
147
148  private:
149   std::list<std::vector<XrSwapchainImageOpenGLKHR>> m_image_cache;
150 };
151
152 #ifdef WIN32
153 class GHOST_XrGraphicsBindingD3D : public GHOST_IXrGraphicsBinding {
154  public:
155   bool checkVersionRequirements(GHOST_Context * /*ghost_ctx*/,
156                                 XrInstance /*instance*/,
157                                 XrSystemId /*system_id*/,
158                                 std::string * /*r_requirement_info*/) const override
159   {
160     // TODO
161   }
162
163   void initFromGhostContext(GHOST_Context *ghost_ctx) override
164   {
165     GHOST_ContextD3D *ctx_d3d = static_cast<GHOST_ContextD3D *>(ghost_ctx);
166
167     oxr_binding.d3d11.type = XR_TYPE_GRAPHICS_BINDING_D3D11_KHR;
168     oxr_binding.d3d11.device = ctx_d3d->m_device;
169     m_ghost_ctx = ctx_d3d;
170   }
171
172   bool chooseSwapchainFormat(const std::vector<int64_t> &runtime_formats,
173                              int64_t *r_result) const override
174   {
175     std::vector<int64_t> gpu_binding_formats = {DXGI_FORMAT_R8G8B8A8_UNORM};
176     return choose_swapchain_format_from_candidates(gpu_binding_formats, runtime_formats, r_result);
177   }
178
179   std::vector<XrSwapchainImageBaseHeader *> createSwapchainImages(uint32_t image_count) override
180   {
181     std::vector<XrSwapchainImageD3D11KHR> d3d_images(image_count);
182     std::vector<XrSwapchainImageBaseHeader *> base_images;
183
184     // Need to return vector of base header pointers, so of a different type. Need to build a new
185     // list with this type, and keep the initial one alive.
186     for (XrSwapchainImageD3D11KHR &image : d3d_images) {
187       image.type = XR_TYPE_SWAPCHAIN_IMAGE_D3D11_KHR;
188       base_images.push_back(reinterpret_cast<XrSwapchainImageBaseHeader *>(&image));
189     }
190
191     // Keep alive.
192     m_image_cache.push_back(std::move(d3d_images));
193
194     return base_images;
195   }
196
197   void drawViewBegin(XrSwapchainImageBaseHeader * /*swapchain_image*/) override
198   {
199   }
200   void drawViewEnd(XrSwapchainImageBaseHeader *swapchain_image, GHOST_Context *ogl_ctx) override
201   {
202     XrSwapchainImageD3D11KHR *d3d_swapchain_image = reinterpret_cast<XrSwapchainImageD3D11KHR *>(
203         swapchain_image);
204
205 #  if 0
206     /* Ideally we'd just create a render target view for the OpenXR swapchain image texture and
207      * blit from the OpenGL context into it. The NV_DX_interop extension doesn't want to work with
208      * this though. At least not with Optimus hardware. See:
209      * https://github.com/mpv-player/mpv/issues/2949#issuecomment-197262807.
210      * Note: Even if this worked, the blitting code only supports one shared resource by now, we'd
211      * need at least two (for each eye). We could also entirely re-register shared resources all
212      * the time. Also, the runtime might recreate the swapchain image so the shared resource would
213      * have to be re-registered then as well. */
214
215     ID3D11RenderTargetView *rtv;
216     CD3D11_RENDER_TARGET_VIEW_DESC rtv_desc(D3D11_RTV_DIMENSION_TEXTURE2D,
217                                             DXGI_FORMAT_R8G8B8A8_UNORM);
218     D3D11_TEXTURE2D_DESC tex_desc;
219
220     d3d_swapchain_image->texture->GetDesc(&tex_desc);
221
222     m_ghost_ctx->m_device->CreateRenderTargetView(d3d_swapchain_image->texture, &rtv_desc, &rtv);
223     m_ghost_ctx->blitOpenGLOffscreenContext(ogl_ctx, rtv, tex_desc.Width, tex_desc.Height);
224 #  else
225     ID3D11Resource *res;
226     ID3D11Texture2D *tex;
227     D3D11_TEXTURE2D_DESC tex_desc;
228
229     d3d_swapchain_image->texture->GetDesc(&tex_desc);
230
231     ogl_ctx->activateDrawingContext();
232     m_ghost_ctx->blitOpenGLOffscreenContext(ogl_ctx, tex_desc.Width, tex_desc.Height);
233
234     m_ghost_ctx->m_backbuffer_view->GetResource(&res);
235     res->QueryInterface<ID3D11Texture2D>(&tex);
236
237     m_ghost_ctx->m_device_ctx->OMSetRenderTargets(0, nullptr, nullptr);
238     m_ghost_ctx->m_device_ctx->CopyResource(d3d_swapchain_image->texture, tex);
239
240     res->Release();
241     tex->Release();
242 #  endif
243   }
244
245  private:
246   GHOST_ContextD3D *m_ghost_ctx;
247   std::list<std::vector<XrSwapchainImageD3D11KHR>> m_image_cache;
248 };
249 #endif  // WIN32
250
251 std::unique_ptr<GHOST_IXrGraphicsBinding> GHOST_XrGraphicsBindingCreateFromType(
252     GHOST_TXrGraphicsBinding type)
253 {
254   switch (type) {
255     case GHOST_kXrGraphicsOpenGL:
256       return std::unique_ptr<GHOST_XrGraphicsBindingOpenGL>(new GHOST_XrGraphicsBindingOpenGL());
257 #ifdef WIN32
258     case GHOST_kXrGraphicsD3D11:
259       return std::unique_ptr<GHOST_XrGraphicsBindingD3D>(new GHOST_XrGraphicsBindingD3D());
260 #endif
261     default:
262       return nullptr;
263   }
264 }