Merge branch 'master' into soc-2019-openxr
[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
161     GHOST_ContextD3D *ctx_dx = static_cast<GHOST_ContextD3D *>(ghost_ctx);
162     XrGraphicsRequirementsD3D11KHR gpu_requirements{XR_TYPE_GRAPHICS_REQUIREMENTS_D3D11_KHR};
163
164     xrGetD3D11GraphicsRequirementsKHR(instance, system_id, &gpu_requirements);
165
166     if (r_requirement_info) {
167       std::ostringstream strstream;
168       strstream << "Min DirectX 11 Feature Level " << gpu_requirements.minFeatureLevel
169                 << std::endl;
170
171       *r_requirement_info = std::move(strstream.str());
172     }
173
174     return ctx_dx->m_device->GetFeatureLevel() >= gpu_requirements.minFeatureLevel;
175   }
176
177   void initFromGhostContext(GHOST_Context *ghost_ctx) override
178   {
179     GHOST_ContextD3D *ctx_d3d = static_cast<GHOST_ContextD3D *>(ghost_ctx);
180
181     oxr_binding.d3d11.type = XR_TYPE_GRAPHICS_BINDING_D3D11_KHR;
182     oxr_binding.d3d11.device = ctx_d3d->m_device;
183     m_ghost_ctx = ctx_d3d;
184   }
185
186   bool chooseSwapchainFormat(const std::vector<int64_t> &runtime_formats,
187                              int64_t *r_result) const override
188   {
189     std::vector<int64_t> gpu_binding_formats = {DXGI_FORMAT_R8G8B8A8_UNORM};
190     return choose_swapchain_format_from_candidates(gpu_binding_formats, runtime_formats, r_result);
191   }
192
193   std::vector<XrSwapchainImageBaseHeader *> createSwapchainImages(uint32_t image_count) override
194   {
195     std::vector<XrSwapchainImageD3D11KHR> d3d_images(image_count);
196     std::vector<XrSwapchainImageBaseHeader *> base_images;
197
198     // Need to return vector of base header pointers, so of a different type. Need to build a new
199     // list with this type, and keep the initial one alive.
200     for (XrSwapchainImageD3D11KHR &image : d3d_images) {
201       image.type = XR_TYPE_SWAPCHAIN_IMAGE_D3D11_KHR;
202       base_images.push_back(reinterpret_cast<XrSwapchainImageBaseHeader *>(&image));
203     }
204
205     // Keep alive.
206     m_image_cache.push_back(std::move(d3d_images));
207
208     return base_images;
209   }
210
211   void drawViewBegin(XrSwapchainImageBaseHeader * /*swapchain_image*/) override
212   {
213   }
214   void drawViewEnd(XrSwapchainImageBaseHeader *swapchain_image, GHOST_Context *ogl_ctx) override
215   {
216     XrSwapchainImageD3D11KHR *d3d_swapchain_image = reinterpret_cast<XrSwapchainImageD3D11KHR *>(
217         swapchain_image);
218
219 #  if 0
220     /* Ideally we'd just create a render target view for the OpenXR swapchain image texture and
221      * blit from the OpenGL context into it. The NV_DX_interop extension doesn't want to work with
222      * this though. At least not with Optimus hardware. See:
223      * https://github.com/mpv-player/mpv/issues/2949#issuecomment-197262807.
224      * Note: Even if this worked, the blitting code only supports one shared resource by now, we'd
225      * need at least two (for each eye). We could also entirely re-register shared resources all
226      * the time. Also, the runtime might recreate the swapchain image so the shared resource would
227      * have to be re-registered then as well. */
228
229     ID3D11RenderTargetView *rtv;
230     CD3D11_RENDER_TARGET_VIEW_DESC rtv_desc(D3D11_RTV_DIMENSION_TEXTURE2D,
231                                             DXGI_FORMAT_R8G8B8A8_UNORM);
232     D3D11_TEXTURE2D_DESC tex_desc;
233
234     d3d_swapchain_image->texture->GetDesc(&tex_desc);
235
236     m_ghost_ctx->m_device->CreateRenderTargetView(d3d_swapchain_image->texture, &rtv_desc, &rtv);
237     m_ghost_ctx->blitOpenGLOffscreenContext(ogl_ctx, rtv, tex_desc.Width, tex_desc.Height);
238 #  else
239     ID3D11Resource *res;
240     ID3D11Texture2D *tex;
241     D3D11_TEXTURE2D_DESC tex_desc;
242
243     d3d_swapchain_image->texture->GetDesc(&tex_desc);
244
245     ogl_ctx->activateDrawingContext();
246     m_ghost_ctx->blitOpenGLOffscreenContext(ogl_ctx, tex_desc.Width, tex_desc.Height);
247
248     m_ghost_ctx->m_backbuffer_view->GetResource(&res);
249     res->QueryInterface<ID3D11Texture2D>(&tex);
250
251     m_ghost_ctx->m_device_ctx->OMSetRenderTargets(0, nullptr, nullptr);
252     m_ghost_ctx->m_device_ctx->CopyResource(d3d_swapchain_image->texture, tex);
253
254     res->Release();
255     tex->Release();
256 #  endif
257   }
258
259  private:
260   GHOST_ContextD3D *m_ghost_ctx;
261   std::list<std::vector<XrSwapchainImageD3D11KHR>> m_image_cache;
262 };
263 #endif  // WIN32
264
265 std::unique_ptr<GHOST_IXrGraphicsBinding> GHOST_XrGraphicsBindingCreateFromType(
266     GHOST_TXrGraphicsBinding type)
267 {
268   switch (type) {
269     case GHOST_kXrGraphicsOpenGL:
270       return std::unique_ptr<GHOST_XrGraphicsBindingOpenGL>(new GHOST_XrGraphicsBindingOpenGL());
271 #ifdef WIN32
272     case GHOST_kXrGraphicsD3D11:
273       return std::unique_ptr<GHOST_XrGraphicsBindingD3D>(new GHOST_XrGraphicsBindingD3D());
274 #endif
275     default:
276       return nullptr;
277   }
278 }