[Bf-blender-cvs] [109be29e42b] soc-2019-openxr: Draw offscreen viewport in XR session surface callback

Julian Eisel noreply at git.blender.org
Sat Jun 29 02:31:26 CEST 2019


Commit: 109be29e42bbea92fa1c993833ffd40af75ab8af
Author: Julian Eisel
Date:   Sat Jun 29 02:12:22 2019 +0200
Branches: soc-2019-openxr
https://developer.blender.org/rB109be29e42bbea92fa1c993833ffd40af75ab8af

Draw offscreen viewport in XR session surface callback

Not visible yet, but it should draw in the offscreen. The way this is
now, we don't depend on the Window->Workspace->bScreen->... chain. We
simply draw an offscreen viewport in the draw callback of the XR session
surface.

The drawing also uses view and projection matrices from OpenXR (or
calculated from OpenXR data).

===================================================================

M	intern/ghost/GHOST_C-api.h
M	intern/ghost/intern/GHOST_XrSession.cpp
M	source/blender/blenlib/BLI_math_geom.h
M	source/blender/blenlib/intern/math_geom.c
M	source/blender/editors/include/ED_view3d.h
M	source/blender/editors/space_view3d/view3d_draw.c
M	source/blender/windowmanager/intern/wm_xr.c

===================================================================

diff --git a/intern/ghost/GHOST_C-api.h b/intern/ghost/GHOST_C-api.h
index 305c6ce5282..3f3c25eca43 100644
--- a/intern/ghost/GHOST_C-api.h
+++ b/intern/ghost/GHOST_C-api.h
@@ -1010,8 +1010,17 @@ typedef struct {
 } GHOST_XrContextCreateInfo;
 
 typedef struct {
-  // Required info to build matrices goes here.
-  int dummy;
+  int width, height;
+
+  struct {
+    float position[3];
+    float quat[4];
+  } pose;
+
+  struct {
+    float angle_left, angle_right;
+    float angle_up, angle_down;
+  } fov;
 } GHOST_XrDrawViewInfo;
 
 /* xr-context */
diff --git a/intern/ghost/intern/GHOST_XrSession.cpp b/intern/ghost/intern/GHOST_XrSession.cpp
index 9040026cfa8..2ca2e38efd2 100644
--- a/intern/ghost/intern/GHOST_XrSession.cpp
+++ b/intern/ghost/intern/GHOST_XrSession.cpp
@@ -241,6 +241,22 @@ static void drawing_end(GHOST_XrContext *xr_context,
   xr_context->draw_frame = nullptr;
 }
 
+static void ghost_xr_draw_view_info_from_view(const XrView &view, GHOST_XrDrawViewInfo &r_info)
+{
+  r_info.pose.position[0] = view.pose.position.x;
+  r_info.pose.position[1] = view.pose.position.y;
+  r_info.pose.position[2] = view.pose.position.z;
+  r_info.pose.quat[0] = view.pose.orientation.x;
+  r_info.pose.quat[1] = view.pose.orientation.y;
+  r_info.pose.quat[2] = view.pose.orientation.z;
+  r_info.pose.quat[3] = view.pose.orientation.w;
+
+  r_info.fov.angle_left = view.fov.angleLeft;
+  r_info.fov.angle_right = view.fov.angleRight;
+  r_info.fov.angle_up = view.fov.angleUp;
+  r_info.fov.angle_down = view.fov.angleDown;
+}
+
 static void draw_view(GHOST_XrContext *xr_context,
                       OpenXRData *oxr,
                       XrSwapchain swapchain,
@@ -269,6 +285,10 @@ static void draw_view(GHOST_XrContext *xr_context,
 
   swapchain_image = oxr->swapchain_images[swapchain][swapchain_idx];
 
+  draw_view_info.width = oxr->swapchain_image_width;
+  draw_view_info.height = oxr->swapchain_image_height;
+  ghost_xr_draw_view_info_from_view(view, draw_view_info);
+
   xr_context->gpu_binding->drawViewBegin(swapchain_image);
   xr_context->draw_view_fn(&draw_view_info, draw_customdata);
   xr_context->gpu_binding->drawViewEnd(swapchain_image);
diff --git a/source/blender/blenlib/BLI_math_geom.h b/source/blender/blenlib/BLI_math_geom.h
index 484d5af194d..18689cca55a 100644
--- a/source/blender/blenlib/BLI_math_geom.h
+++ b/source/blender/blenlib/BLI_math_geom.h
@@ -603,6 +603,13 @@ void perspective_m4(float mat[4][4],
                     const float top,
                     const float nearClip,
                     const float farClip);
+void perspective_m4_fov(float mat[4][4],
+                        const float angle_left,
+                        const float angle_right,
+                        const float angle_up,
+                        const float angle_down,
+                        const float nearClip,
+                        const float farClip);
 void orthographic_m4(float mat[4][4],
                      const float left,
                      const float right,
diff --git a/source/blender/blenlib/intern/math_geom.c b/source/blender/blenlib/intern/math_geom.c
index 5dbd2a52d07..367494537e3 100644
--- a/source/blender/blenlib/intern/math_geom.c
+++ b/source/blender/blenlib/intern/math_geom.c
@@ -4324,6 +4324,33 @@ void perspective_m4(float mat[4][4],
       mat[3][3] = 0.0f;
 }
 
+void perspective_m4_fov(float mat[4][4],
+                        const float angle_left,
+                        const float angle_right,
+                        const float angle_up,
+                        const float angle_down,
+                        const float nearClip,
+                        const float farClip)
+{
+  const float tan_angle_left = tanf(angle_left);
+  const float tan_angle_right = tanf(angle_right);
+  const float tan_angle_up = tanf(angle_up);
+  const float tan_angle_down = tanf(angle_down);
+  const float Xdelta = tan_angle_right - tan_angle_left;
+  const float Ydelta = tan_angle_up - tan_angle_down;
+
+  mat[0][0] = 2 / Xdelta;
+  mat[1][1] = 2 / Ydelta;
+  mat[2][0] = (tan_angle_right + tan_angle_left) / Xdelta;
+  mat[2][1] = (tan_angle_up + tan_angle_down) / Ydelta;
+  mat[2][2] = -(farClip + nearClip) / (farClip - nearClip);
+  mat[2][3] = -1;
+  mat[3][2] = -(farClip * (nearClip + nearClip)) / (farClip - nearClip);
+
+  mat[0][1] = mat[0][2] = mat[0][3] = mat[1][0] = mat[1][2] = mat[1][3] = mat[3][0] = mat[3][1] =
+      mat[3][3] = 0.0f;
+}
+
 /* translate a matrix created by orthographic_m4 or perspective_m4 in XY coords
  * (used to jitter the view) */
 void window_translate_m4(float winmat[4][4], float perspmat[4][4], const float x, const float y)
diff --git a/source/blender/editors/include/ED_view3d.h b/source/blender/editors/include/ED_view3d.h
index 93268456277..ebeeea3fa4c 100644
--- a/source/blender/editors/include/ED_view3d.h
+++ b/source/blender/editors/include/ED_view3d.h
@@ -587,6 +587,24 @@ void ED_view3d_draw_offscreen(struct Depsgraph *depsgraph,
                               const bool do_color_managment,
                               struct GPUOffScreen *ofs,
                               struct GPUViewport *viewport);
+void ED_view3d_draw_offscreen_simple(struct Depsgraph *depsgraph,
+                                     struct Scene *scene,
+                                     struct View3DShading *shading_override,
+                                     int drawtype,
+                                     int winx,
+                                     int winy,
+                                     unsigned int draw_flags,
+                                     float viewmat[4][4],
+                                     float winmat[4][4],
+                                     float clip_start,
+                                     float clip_end,
+                                     float lens,
+                                     bool do_sky,
+                                     bool is_persp,
+                                     const char *viewname,
+                                     const bool do_color_management,
+                                     struct GPUOffScreen *ofs,
+                                     struct GPUViewport *viewport);
 void ED_view3d_draw_setup_view(struct wmWindow *win,
                                struct Depsgraph *depsgraph,
                                struct Scene *scene,
diff --git a/source/blender/editors/space_view3d/view3d_draw.c b/source/blender/editors/space_view3d/view3d_draw.c
index d14a6870a6c..add6d33cdd6 100644
--- a/source/blender/editors/space_view3d/view3d_draw.c
+++ b/source/blender/editors/space_view3d/view3d_draw.c
@@ -1590,6 +1590,75 @@ void ED_view3d_draw_offscreen(Depsgraph *depsgraph,
   G.f &= ~G_FLAG_RENDER_VIEWPORT;
 }
 
+void ED_view3d_draw_offscreen_simple(Depsgraph *depsgraph,
+                                     Scene *scene,
+                                     View3DShading *shading_override,
+                                     int drawtype,
+                                     int winx,
+                                     int winy,
+                                     uint draw_flags,
+                                     float viewmat[4][4],
+                                     float winmat[4][4],
+                                     float clip_start,
+                                     float clip_end,
+                                     float lens,
+                                     bool do_sky,
+                                     bool is_persp,
+                                     const char *viewname,
+                                     const bool do_color_management,
+                                     GPUOffScreen *ofs,
+                                     GPUViewport *viewport)
+{
+  View3D v3d = {NULL};
+  ARegion ar = {NULL};
+  RegionView3D rv3d = {{{0}}};
+
+  /* connect data */
+  v3d.regionbase.first = v3d.regionbase.last = &ar;
+  ar.regiondata = &rv3d;
+  ar.regiontype = RGN_TYPE_WINDOW;
+
+  View3DShading *source_shading_settings = &scene->display.shading;
+  if (draw_flags & V3D_OFSDRAW_OVERRIDE_SCENE_SETTINGS && shading_override != NULL) {
+    source_shading_settings = shading_override;
+  }
+  memcpy(&v3d.shading, source_shading_settings, sizeof(View3DShading));
+  v3d.shading.type = drawtype;
+
+  if (drawtype == OB_MATERIAL) {
+    v3d.shading.flag = V3D_SHADING_SCENE_WORLD | V3D_SHADING_SCENE_LIGHTS;
+  }
+
+  v3d.flag2 = V3D_HIDE_OVERLAYS;
+
+  if (draw_flags & V3D_OFSDRAW_SHOW_ANNOTATION) {
+    v3d.flag2 |= V3D_SHOW_ANNOTATION;
+  }
+
+  v3d.shading.background_type = V3D_SHADING_BACKGROUND_WORLD;
+
+  rv3d.persp = RV3D_PERSP;
+  v3d.clip_start = clip_start;
+  v3d.clip_end = clip_end;
+  v3d.lens = lens;
+
+  ED_view3d_draw_offscreen(depsgraph,
+                           scene,
+                           drawtype,
+                           &v3d,
+                           &ar,
+                           winx,
+                           winy,
+                           viewmat,
+                           winmat,
+                           do_sky,
+                           is_persp,
+                           viewname,
+                           do_color_management,
+                           ofs,
+                           viewport);
+}
+
 /**
  * Utility func for ED_view3d_draw_offscreen
  *
diff --git a/source/blender/windowmanager/intern/wm_xr.c b/source/blender/windowmanager/intern/wm_xr.c
index f6f118118ff..a4ba130f951 100644
--- a/source/blender/windowmanager/intern/wm_xr.c
+++ b/source/blender/windowmanager/intern/wm_xr.c
@@ -22,10 +22,21 @@
 #include "BKE_global.h"
 #include "BKE_main.h"
 
+#include "BLI_math_geom.h"
+#include "BLI_math_matrix.h"
+
+#include "DNA_object_types.h"
+
+#include "DRW_engine.h"
+
+#include "ED_view3d.h"
+
 #include "GHOST_C-api.h"
 
 #include "GPU_context.h"
 #include "GPU_draw.h"
+#include "GPU_matrix.h"
+#include "GPU_viewport.h"
 
 #include "MEM_guardedalloc.h"
 
@@ -137,8 +148,64 @@ static wmSurface *wm_xr_session_surface_create(wmWindowManager *wm, unsigned int
   return surface;
 }
 
-static void wm_xr_draw_view(const GHOST_XrDrawViewInfo *UNUSED(draw_view), void *customdata)
+static void wm_xr_draw_view(c

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list