[Bf-blender-cvs] [cb75a83df02] xr-actions-D9124: XR: Draw controllers via draw handler

Peter Kim noreply at git.blender.org
Wed Oct 21 16:59:48 CEST 2020


Commit: cb75a83df02bdd866f1e63b08c2a1a764a0c4db3
Author: Peter Kim
Date:   Wed Oct 21 23:47:29 2020 +0900
Branches: xr-actions-D9124
https://developer.blender.org/rBcb75a83df02bdd866f1e63b08c2a1a764a0c4db3

XR: Draw controllers via draw handler

Callbacks for controllers and other custom drawing will be registered
with the XR surface's region type (RGN_TYPE_XR_DISPLAY) and called
post-view.

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

M	source/blender/draw/intern/draw_manager.c
M	source/blender/editors/include/ED_space_api.h
M	source/blender/editors/space_api/spacetypes.c
M	source/blender/makesdna/DNA_screen_types.h
M	source/blender/makesrna/intern/rna_screen.c
M	source/blender/windowmanager/WM_api.h
M	source/blender/windowmanager/xr/intern/wm_xr_draw.c
M	source/blender/windowmanager/xr/intern/wm_xr_intern.h
M	source/blender/windowmanager/xr/intern/wm_xr_session.c

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

diff --git a/source/blender/draw/intern/draw_manager.c b/source/blender/draw/intern/draw_manager.c
index e31b7a17134..e418571e094 100644
--- a/source/blender/draw/intern/draw_manager.c
+++ b/source/blender/draw/intern/draw_manager.c
@@ -51,6 +51,7 @@
 #include "BKE_pbvh.h"
 #include "BKE_pointcache.h"
 #include "BKE_pointcloud.h"
+#include "BKE_screen.h"
 #include "BKE_volume.h"
 
 #include "DNA_camera_types.h"
@@ -1462,13 +1463,18 @@ void DRW_draw_callbacks_post_scene(void)
       ED_annotation_draw_view3d(DEG_get_input_scene(depsgraph), depsgraph, v3d, region, true);
     }
 
-#ifdef WITH_XR_OPENXR
-    /* Controllers. */
+    /* Callbacks (controllers, custom draw functions). */
     if ((v3d->flag2 & V3D_XR_SHOW_CONTROLLERS) != 0) {
-      GPU_depth_test(GPU_DEPTH_ALWAYS);
-      WM_xr_draw_controllers();
+      ARegionType *art = WM_xr_surface_region_type_get();
+      if (art) {
+        GPU_depth_test(GPU_DEPTH_NONE);
+        GPU_apply_state();
+
+        ED_region_surface_draw_cb_draw(art, REGION_DRAW_POST_VIEW);
+
+        DRW_state_reset();
+      }
     }
-#endif
 
     GPU_depth_test(GPU_DEPTH_LESS_EQUAL);
   }
diff --git a/source/blender/editors/include/ED_space_api.h b/source/blender/editors/include/ED_space_api.h
index 47c2d4f790c..95c36fc69a0 100644
--- a/source/blender/editors/include/ED_space_api.h
+++ b/source/blender/editors/include/ED_space_api.h
@@ -71,8 +71,9 @@ void *ED_region_draw_cb_activate(struct ARegionType *art,
                                  void (*draw)(const struct bContext *, struct ARegion *, void *),
                                  void *customdata,
                                  int type);
-void ED_region_draw_cb_draw(const struct bContext *, struct ARegion *, int);
-void ED_region_draw_cb_exit(struct ARegionType *, void *);
+void ED_region_draw_cb_draw(const struct bContext *C, struct ARegion *region, int type);
+void ED_region_surface_draw_cb_draw(struct ARegionType *art, int type);
+void ED_region_draw_cb_exit(struct ARegionType *art, void *handle);
 /* generic callbacks */
 /* ed_util.c */
 void ED_region_draw_mouse_line_cb(const struct bContext *C,
diff --git a/source/blender/editors/space_api/spacetypes.c b/source/blender/editors/space_api/spacetypes.c
index b89c8cb2193..e1783358158 100644
--- a/source/blender/editors/space_api/spacetypes.c
+++ b/source/blender/editors/space_api/spacetypes.c
@@ -268,12 +268,12 @@ void ED_region_draw_cb_exit(ARegionType *art, void *handle)
   }
 }
 
-void ED_region_draw_cb_draw(const bContext *C, ARegion *region, int type)
+static void ed_region_draw_cb_draw(const bContext *C, ARegion *region, ARegionType *art, int type)
 {
   RegionDrawCB *rdc;
   bool has_drawn_something = false;
 
-  for (rdc = region->type->drawcalls.first; rdc; rdc = rdc->next) {
+  for (rdc = art->drawcalls.first; rdc; rdc = rdc->next) {
     if (rdc->type == type) {
       rdc->draw(C, region, rdc->customdata);
       has_drawn_something = true;
@@ -285,6 +285,16 @@ void ED_region_draw_cb_draw(const bContext *C, ARegion *region, int type)
   }
 }
 
+void ED_region_draw_cb_draw(const bContext *C, ARegion *region, int type)
+{
+  ed_region_draw_cb_draw(C, region, region->type, type);
+}
+
+void ED_region_surface_draw_cb_draw(ARegionType *art, int type)
+{
+  ed_region_draw_cb_draw(NULL, NULL, art, type);
+}
+
 /* ********************* space template *********************** */
 /* forward declare */
 void ED_spacetype_xxx(void);
diff --git a/source/blender/makesdna/DNA_screen_types.h b/source/blender/makesdna/DNA_screen_types.h
index f0ff02d3668..c650b441895 100644
--- a/source/blender/makesdna/DNA_screen_types.h
+++ b/source/blender/makesdna/DNA_screen_types.h
@@ -636,6 +636,8 @@ typedef enum eRegionType {
   RGN_TYPE_EXECUTE = 10,
   RGN_TYPE_FOOTER = 11,
   RGN_TYPE_TOOL_HEADER = 12,
+  /* Region representing an XR headset's display (internally, corresponds to an XR surface). */
+  RGN_TYPE_XR_DISPLAY = 13,
 
 #define RGN_TYPE_LEN (RGN_TYPE_TOOL_HEADER + 1)
 } eRegionType;
diff --git a/source/blender/makesrna/intern/rna_screen.c b/source/blender/makesrna/intern/rna_screen.c
index ab84dcb0aba..7e066ef3a72 100644
--- a/source/blender/makesrna/intern/rna_screen.c
+++ b/source/blender/makesrna/intern/rna_screen.c
@@ -46,6 +46,7 @@ const EnumPropertyItem rna_enum_region_type_items[] = {
     {RGN_TYPE_EXECUTE, "EXECUTE", 0, "Execute Buttons", ""},
     {RGN_TYPE_FOOTER, "FOOTER", 0, "Footer", ""},
     {RGN_TYPE_TOOL_HEADER, "TOOL_HEADER", 0, "Tool Header", ""},
+    {RGN_TYPE_XR_DISPLAY, "XR_DISPLAY", 0, "XR Display", ""},
     {0, NULL, 0, NULL, NULL},
 };
 
diff --git a/source/blender/windowmanager/WM_api.h b/source/blender/windowmanager/WM_api.h
index a71bf879f81..eba9259783a 100644
--- a/source/blender/windowmanager/WM_api.h
+++ b/source/blender/windowmanager/WM_api.h
@@ -944,6 +944,8 @@ bool WM_xr_session_state_controller_pose_rotation_get(const wmXrData *xr,
                                                       unsigned int subaction_idx,
                                                       float r_rotation[4]);
 
+struct ARegionType *WM_xr_surface_region_type_get(void);
+
 /* wm_xr_actions.c */
 /* XR action functions to be called pre-XR session start.
  * Note: The "destroy" functions can also be called post-session start. */
@@ -999,9 +1001,6 @@ void WM_xr_haptic_action_stop(wmXrData *xr,
                               const char *action_name,
                               unsigned int count,
                               const char *const *subaction_paths);
-
-/* wm_xr_draw.c */
-void WM_xr_draw_controllers(void /*const struct wmXrSessionState *state*/);
 #endif
 
 #ifdef __cplusplus
diff --git a/source/blender/windowmanager/xr/intern/wm_xr_draw.c b/source/blender/windowmanager/xr/intern/wm_xr_draw.c
index 1c2fa9b0451..a10d9b9d173 100644
--- a/source/blender/windowmanager/xr/intern/wm_xr_draw.c
+++ b/source/blender/windowmanager/xr/intern/wm_xr_draw.c
@@ -24,6 +24,8 @@
 
 #include <string.h>
 
+#include "BKE_context.h"
+
 #include "BLI_math.h"
 
 #include "ED_view3d_offscreen.h"
@@ -38,9 +40,6 @@
 #include "wm_surface.h"
 #include "wm_xr_intern.h"
 
-/* TODO_XR: Remove this (see WM_xr_draw_controllers()). */
-wmXrSessionState *g_session_state = NULL;
-
 void wm_xr_pose_to_viewmat(const GHOST_XrPose *pose, float r_viewmat[4][4])
 {
   float iquat[4];
@@ -138,9 +137,6 @@ void wm_xr_draw_view(const GHOST_XrDrawViewInfo *draw_view, void *customdata)
   /* Some systems have drawing glitches without this. */
   GPU_clear_depth(1.0f);
 
-  /* TODO_XR: Remove this. */
-  g_session_state = session_state;
-
   /* Draws the view into the surface_data->viewport's framebuffers */
   ED_view3d_draw_offscreen_simple(draw_data->depsgraph,
                                   draw_data->scene,
@@ -175,16 +171,12 @@ void wm_xr_draw_view(const GHOST_XrDrawViewInfo *draw_view, void *customdata)
   wm_xr_draw_viewport_buffers_to_active_framebuffer(xr_data->runtime, surface_data, draw_view);
 }
 
-/* This function is called from the post-scene callback portion of the offscreen render loop.
- * TODO_XR: Find a way to pass the session state (needed to access the controller matrices)
- * through the render loop. */
-void WM_xr_draw_controllers(void /*const wmXrSessionState *state*/)
+void wm_xr_draw_controllers(const bContext *UNUSED(C), ARegion *UNUSED(region), void *customdata)
 {
-  /* TODO_XR: Remove this. */
-  if (!g_session_state) {
-    return;
-  }
-  const wmXrSessionState *state = g_session_state;
+  const wmXrSessionState *state = customdata;
+
+  const eGPUDepthTest depth_test_prev = GPU_depth_test_get();
+  GPU_depth_test(GPU_DEPTH_ALWAYS);
 
   /* For now, just draw controller axes. In the future this can be replaced
    * with actual controller geometry. */
@@ -238,4 +230,6 @@ void WM_xr_draw_controllers(void /*const wmXrSessionState *state*/)
   }
 
   immUnbindProgram();
+
+  GPU_depth_test(depth_test_prev);
 }
diff --git a/source/blender/windowmanager/xr/intern/wm_xr_intern.h b/source/blender/windowmanager/xr/intern/wm_xr_intern.h
index c00b8f30424..962af41e581 100644
--- a/source/blender/windowmanager/xr/intern/wm_xr_intern.h
+++ b/source/blender/windowmanager/xr/intern/wm_xr_intern.h
@@ -100,8 +100,14 @@ typedef struct wmXrRuntimeData {
 typedef struct {
   struct GPUOffScreen *offscreen;
   struct GPUViewport *viewport;
+
   /** XR events. */
   ListBase events;
+
+  /** Dummy region type. Used to add draw callbacks. */
+  struct ARegionType *art;
+  /** Controller draw callback handle. */
+  void *controller_draw_handle;
 } wmXrSurfaceData;
 
 typedef struct wmXrDrawData {
@@ -181,3 +187,4 @@ void wm_xr_session_controller_data_clear(unsigned int count_subaction_paths,
 void wm_xr_pose_to_viewmat(const GHOST_XrPose *pose, float r_viewmat[4][4]);
 void wm_xr_controller_pose_to_mat(const GHOST_XrPose *pose, float r_mat[4][4]);
 void wm_xr_draw_view(const GHOST_XrDrawViewInfo *draw_view, void *customdata);
+void wm_xr_draw_controllers(const struct bContext *C, struct ARegion *region, void *customdata);
diff --git a/source/blender/windowmanager/xr/intern/wm_xr_session.c b/source/blender/windowmanager/xr/intern/wm_xr_session.c
index 412502ca972..6b8c3696473 100644
--- a/source/blender/windowmanager/xr/intern/wm_xr_session.c
+++ b/source/blender/windowmanager/xr/intern/wm_xr_session.c
@@ -23,6 +23,7 @@
 #include "BKE_layer.h"
 #include "BKE_main.h"
 #include "BKE_scene.h"
+#include "BKE_screen.h"
 
 #include "BLI_ghash.h"
 #include "BLI_listbase.h"
@@ -36,6 +37,7 @@
 #include "DRW_engine.h"
 
 #include "ED_object.h"
+#include "ED_space_api.h"
 
 #include "GHOST_C-api.h"
 
@@ -792,6 +794,17 @@ void wm_xr_session_controller_data_populate(const wmXrAction *controller_pose_ac
     UNUSED_VARS(C);
 #endif
   }
+
+  /* Activate draw callback. */
+  if (g_xr_surface) {
+    wmXrSurfaceData *surface_data = g_xr_surface->customdata;
+    if (surface_data && !surface_data->controller_draw_handle) {
+      if (surface_data->art) {
+        surface_data->controller_draw_handle = ED_region_draw_cb_activate(
+            surface_data->art, wm_xr_draw_controllers, state, REGION_DRAW_POST_VIEW);
+      }
+    }
+  }
 }
 
 void wm_xr_session_controller_data_clear(unsigned int count_subaction_paths,
@@ -8

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list