Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorPeter Kim <pk15950@gmail.com>2021-10-12 10:18:05 +0300
committerPeter Kim <pk15950@gmail.com>2021-10-12 10:18:05 +0300
commit9dda65455b54336fe3efef91eba9e41866dac1c1 (patch)
tree2f8fd6edf8d190e018b63c6b6bdacb38ab7905bd /source
parentcfa59b3fabe729e49a57154ce21c5a4b88aa5812 (diff)
XR Controller Support Step 4: Controller Drawing
Addresses T77127 (Controller Drawing). Adds VR controller visualization and custom drawing via draw handlers. Add-ons can draw to the XR surface (headset display) and mirror window by adding a View3D draw handler of region type 'XR' and draw type 'POST_VIEW'. Controller drawing and custom overlays can be toggled individually as XR session options, which will be added in a future update to the VR Scene Inspection add-on. For the actual drawing, the OpenXR XR_MSFT_controller_model extension is used to load a glTF model provided by the XR runtime. The model's vertex data is then used to create a GPUBatch in the XR session state. Finally, this batch is drawn via the XR surface draw handler mentioned above. For runtimes that do not support the controller model extension, a a simple fallback shape (sphere) is drawn instead. Reviewed By: Severin, fclem Differential Revision: https://developer.blender.org/D10948
Diffstat (limited to 'source')
-rw-r--r--source/blender/draw/intern/draw_manager.c62
-rw-r--r--source/blender/editors/include/ED_space_api.h5
-rw-r--r--source/blender/editors/include/ED_view3d_offscreen.h3
-rw-r--r--source/blender/editors/space_api/spacetypes.c14
-rw-r--r--source/blender/editors/space_view3d/space_view3d.c5
-rw-r--r--source/blender/editors/space_view3d/view3d_draw.c48
-rw-r--r--source/blender/makesdna/DNA_screen_types.h5
-rw-r--r--source/blender/makesdna/DNA_view3d_enums.h3
-rw-r--r--source/blender/makesdna/DNA_view3d_types.h3
-rw-r--r--source/blender/makesdna/DNA_xr_types.h11
-rw-r--r--source/blender/makesrna/intern/rna_screen.c1
-rw-r--r--source/blender/makesrna/intern/rna_xr.c39
-rw-r--r--source/blender/windowmanager/WM_api.h2
-rw-r--r--source/blender/windowmanager/xr/intern/wm_xr_draw.c203
-rw-r--r--source/blender/windowmanager/xr/intern/wm_xr_intern.h14
-rw-r--r--source/blender/windowmanager/xr/intern/wm_xr_session.c64
16 files changed, 460 insertions, 22 deletions
diff --git a/source/blender/draw/intern/draw_manager.c b/source/blender/draw/intern/draw_manager.c
index b8de92dea7f..c8900d64935 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"
@@ -1418,6 +1419,27 @@ void DRW_draw_callbacks_post_scene(void)
ED_region_draw_cb_draw(DST.draw_ctx.evil_C, DST.draw_ctx.region, REGION_DRAW_POST_VIEW);
+#ifdef WITH_XR_OPENXR
+ /* XR callbacks (controllers, custom draw functions) for session mirror. */
+ if ((v3d->flag & V3D_XR_SESSION_MIRROR) != 0) {
+ if ((v3d->flag2 & V3D_XR_SHOW_CONTROLLERS) != 0) {
+ ARegionType *art = WM_xr_surface_controller_region_type_get();
+ if (art) {
+ ED_region_surface_draw_cb_draw(art, REGION_DRAW_POST_VIEW);
+ }
+ }
+ if ((v3d->flag2 & V3D_XR_SHOW_CUSTOM_OVERLAYS) != 0) {
+ SpaceType *st = BKE_spacetype_from_id(SPACE_VIEW3D);
+ if (st) {
+ ARegionType *art = BKE_regiontype_from_id(st, RGN_TYPE_XR);
+ if (art) {
+ ED_region_surface_draw_cb_draw(art, REGION_DRAW_POST_VIEW);
+ }
+ }
+ }
+ }
+#endif
+
/* Callback can be nasty and do whatever they want with the state.
* Don't trust them! */
DRW_state_reset();
@@ -1464,6 +1486,46 @@ void DRW_draw_callbacks_post_scene(void)
ED_annotation_draw_view3d(DEG_get_input_scene(depsgraph), depsgraph, v3d, region, true);
GPU_depth_test(GPU_DEPTH_LESS_EQUAL);
}
+
+#ifdef WITH_XR_OPENXR
+ if ((v3d->flag & V3D_XR_SESSION_SURFACE) != 0) {
+ DefaultFramebufferList *dfbl = DRW_viewport_framebuffer_list_get();
+
+ DRW_state_reset();
+
+ GPU_framebuffer_bind(dfbl->overlay_fb);
+
+ GPU_matrix_projection_set(rv3d->winmat);
+ GPU_matrix_set(rv3d->viewmat);
+
+ /* XR callbacks (controllers, custom draw functions) for session surface. */
+ if (((v3d->flag2 & V3D_XR_SHOW_CONTROLLERS) != 0) ||
+ ((v3d->flag2 & V3D_XR_SHOW_CUSTOM_OVERLAYS) != 0)) {
+ GPU_depth_test(GPU_DEPTH_NONE);
+ GPU_apply_state();
+
+ if ((v3d->flag2 & V3D_XR_SHOW_CONTROLLERS) != 0) {
+ ARegionType *art = WM_xr_surface_controller_region_type_get();
+ if (art) {
+ ED_region_surface_draw_cb_draw(art, REGION_DRAW_POST_VIEW);
+ }
+ }
+ if ((v3d->flag2 & V3D_XR_SHOW_CUSTOM_OVERLAYS) != 0) {
+ SpaceType *st = BKE_spacetype_from_id(SPACE_VIEW3D);
+ if (st) {
+ ARegionType *art = BKE_regiontype_from_id(st, RGN_TYPE_XR);
+ if (art) {
+ ED_region_surface_draw_cb_draw(art, REGION_DRAW_POST_VIEW);
+ }
+ }
+ }
+
+ DRW_state_reset();
+ }
+
+ GPU_depth_test(GPU_DEPTH_LESS_EQUAL);
+ }
+#endif
}
}
diff --git a/source/blender/editors/include/ED_space_api.h b/source/blender/editors/include/ED_space_api.h
index 1a3aa7e5496..958df8f7707 100644
--- a/source/blender/editors/include/ED_space_api.h
+++ b/source/blender/editors/include/ED_space_api.h
@@ -72,8 +72,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);
void ED_region_draw_cb_remove_by_type(struct ARegionType *art,
void *draw_fn,
void (*free)(void *));
diff --git a/source/blender/editors/include/ED_view3d_offscreen.h b/source/blender/editors/include/ED_view3d_offscreen.h
index c490e96031f..8b695e61a35 100644
--- a/source/blender/editors/include/ED_view3d_offscreen.h
+++ b/source/blender/editors/include/ED_view3d_offscreen.h
@@ -60,7 +60,7 @@ void ED_view3d_draw_offscreen(struct Depsgraph *depsgraph,
void ED_view3d_draw_offscreen_simple(struct Depsgraph *depsgraph,
struct Scene *scene,
struct View3DShading *shading_override,
- int drawtype,
+ eDrawType drawtype,
int winx,
int winy,
unsigned int draw_flags,
@@ -68,6 +68,7 @@ void ED_view3d_draw_offscreen_simple(struct Depsgraph *depsgraph,
const float winmat[4][4],
float clip_start,
float clip_end,
+ bool is_xr_surface,
bool is_image_render,
bool draw_background,
const char *viewname,
diff --git a/source/blender/editors/space_api/spacetypes.c b/source/blender/editors/space_api/spacetypes.c
index 3da283089c5..149067a94fe 100644
--- a/source/blender/editors/space_api/spacetypes.c
+++ b/source/blender/editors/space_api/spacetypes.c
@@ -264,9 +264,9 @@ 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)
{
- LISTBASE_FOREACH_MUTABLE (RegionDrawCB *, rdc, &region->type->drawcalls) {
+ LISTBASE_FOREACH_MUTABLE (RegionDrawCB *, rdc, &art->drawcalls) {
if (rdc->type == type) {
rdc->draw(C, region, rdc->customdata);
@@ -276,6 +276,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);
+}
+
void ED_region_draw_cb_remove_by_type(ARegionType *art, void *draw_fn, void (*free)(void *))
{
LISTBASE_FOREACH_MUTABLE (RegionDrawCB *, rdc, &art->drawcalls) {
diff --git a/source/blender/editors/space_view3d/space_view3d.c b/source/blender/editors/space_view3d/space_view3d.c
index f68a4d78a00..bedc24a6287 100644
--- a/source/blender/editors/space_view3d/space_view3d.c
+++ b/source/blender/editors/space_view3d/space_view3d.c
@@ -1798,5 +1798,10 @@ void ED_spacetype_view3d(void)
art = ED_area_type_hud(st->spaceid);
BLI_addhead(&st->regiontypes, art);
+ /* regions: xr */
+ art = MEM_callocN(sizeof(ARegionType), "spacetype view3d xr region");
+ art->regionid = RGN_TYPE_XR;
+ BLI_addhead(&st->regiontypes, art);
+
BKE_spacetype_register(st);
}
diff --git a/source/blender/editors/space_view3d/view3d_draw.c b/source/blender/editors/space_view3d/view3d_draw.c
index 733ec7e81cd..4da24888e1d 100644
--- a/source/blender/editors/space_view3d/view3d_draw.c
+++ b/source/blender/editors/space_view3d/view3d_draw.c
@@ -339,7 +339,16 @@ static void view3d_xr_mirror_setup(const wmWindowManager *wm,
}
view3d_main_region_setup_view(depsgraph, scene, v3d, region, viewmat, NULL, rect);
- /* Reset overridden View3D data */
+ /* Set draw flags. */
+ SET_FLAG_FROM_TEST(v3d->flag2,
+ (wm->xr.session_settings.draw_flags & V3D_OFSDRAW_XR_SHOW_CONTROLLERS) != 0,
+ V3D_XR_SHOW_CONTROLLERS);
+ SET_FLAG_FROM_TEST(v3d->flag2,
+ (wm->xr.session_settings.draw_flags & V3D_OFSDRAW_XR_SHOW_CUSTOM_OVERLAYS) !=
+ 0,
+ V3D_XR_SHOW_CUSTOM_OVERLAYS);
+
+ /* Reset overridden View3D data. */
v3d->lens = lens_old;
}
#endif /* WITH_XR_OPENXR */
@@ -1757,6 +1766,7 @@ void ED_view3d_draw_offscreen_simple(Depsgraph *depsgraph,
const float winmat[4][4],
float clip_start,
float clip_end,
+ bool is_xr_surface,
bool is_image_render,
bool draw_background,
const char *viewname,
@@ -1786,23 +1796,37 @@ void ED_view3d_draw_offscreen_simple(Depsgraph *depsgraph,
v3d.shading.flag = V3D_SHADING_SCENE_WORLD | V3D_SHADING_SCENE_LIGHTS;
}
- if (draw_flags & V3D_OFSDRAW_SHOW_ANNOTATION) {
- v3d.flag2 |= V3D_SHOW_ANNOTATION;
+ if ((draw_flags & ~V3D_OFSDRAW_OVERRIDE_SCENE_SETTINGS) == V3D_OFSDRAW_NONE) {
+ v3d.flag2 = V3D_HIDE_OVERLAYS;
}
- if (draw_flags & V3D_OFSDRAW_SHOW_GRIDFLOOR) {
- v3d.gridflag |= V3D_SHOW_FLOOR | V3D_SHOW_X | V3D_SHOW_Y;
- v3d.grid = 1.0f;
- v3d.gridlines = 16;
- v3d.gridsubdiv = 10;
-
- /* Show grid, disable other overlays (set all available _HIDE_ flags). */
+ else {
+ if (draw_flags & V3D_OFSDRAW_SHOW_ANNOTATION) {
+ v3d.flag2 |= V3D_SHOW_ANNOTATION;
+ }
+ if (draw_flags & V3D_OFSDRAW_SHOW_GRIDFLOOR) {
+ v3d.gridflag |= V3D_SHOW_FLOOR | V3D_SHOW_X | V3D_SHOW_Y;
+ v3d.grid = 1.0f;
+ v3d.gridlines = 16;
+ v3d.gridsubdiv = 10;
+ }
+ if (draw_flags & V3D_OFSDRAW_SHOW_SELECTION) {
+ v3d.flag |= V3D_SELECT_OUTLINE;
+ }
+ if (draw_flags & V3D_OFSDRAW_XR_SHOW_CONTROLLERS) {
+ v3d.flag2 |= V3D_XR_SHOW_CONTROLLERS;
+ }
+ if (draw_flags & V3D_OFSDRAW_XR_SHOW_CUSTOM_OVERLAYS) {
+ v3d.flag2 |= V3D_XR_SHOW_CUSTOM_OVERLAYS;
+ }
+ /* Disable other overlays (set all available _HIDE_ flags). */
v3d.overlay.flag |= V3D_OVERLAY_HIDE_CURSOR | V3D_OVERLAY_HIDE_TEXT |
V3D_OVERLAY_HIDE_MOTION_PATHS | V3D_OVERLAY_HIDE_BONES |
V3D_OVERLAY_HIDE_OBJECT_XTRAS | V3D_OVERLAY_HIDE_OBJECT_ORIGINS;
v3d.flag |= V3D_HIDE_HELPLINES;
}
- else {
- v3d.flag2 = V3D_HIDE_OVERLAYS;
+
+ if (is_xr_surface) {
+ v3d.flag |= V3D_XR_SESSION_SURFACE;
}
rv3d.persp = RV3D_PERSP;
diff --git a/source/blender/makesdna/DNA_screen_types.h b/source/blender/makesdna/DNA_screen_types.h
index f557890e3e8..86fd6b9744a 100644
--- a/source/blender/makesdna/DNA_screen_types.h
+++ b/source/blender/makesdna/DNA_screen_types.h
@@ -666,8 +666,11 @@ typedef enum eRegion_Type {
RGN_TYPE_EXECUTE = 10,
RGN_TYPE_FOOTER = 11,
RGN_TYPE_TOOL_HEADER = 12,
+ /* Region type used exclusively by internal code and add-ons to register draw callbacks to the XR
+ context (surface, mirror view). Does not represent any real region. */
+ RGN_TYPE_XR = 13,
-#define RGN_TYPE_LEN (RGN_TYPE_TOOL_HEADER + 1)
+#define RGN_TYPE_LEN (RGN_TYPE_XR + 1)
} eRegion_Type;
/* use for function args */
diff --git a/source/blender/makesdna/DNA_view3d_enums.h b/source/blender/makesdna/DNA_view3d_enums.h
index aec52da1bf9..59012a0cd8d 100644
--- a/source/blender/makesdna/DNA_view3d_enums.h
+++ b/source/blender/makesdna/DNA_view3d_enums.h
@@ -30,6 +30,9 @@ typedef enum eV3DOffscreenDrawFlag {
V3D_OFSDRAW_SHOW_ANNOTATION = (1 << 0),
V3D_OFSDRAW_OVERRIDE_SCENE_SETTINGS = (1 << 1),
V3D_OFSDRAW_SHOW_GRIDFLOOR = (1 << 2),
+ V3D_OFSDRAW_SHOW_SELECTION = (1 << 3),
+ V3D_OFSDRAW_XR_SHOW_CONTROLLERS = (1 << 4),
+ V3D_OFSDRAW_XR_SHOW_CUSTOM_OVERLAYS = (1 << 5),
} eV3DOffscreenDrawFlag;
/** #View3DShading.light */
diff --git a/source/blender/makesdna/DNA_view3d_types.h b/source/blender/makesdna/DNA_view3d_types.h
index 4d88f6f0c15..fafc470b95a 100644
--- a/source/blender/makesdna/DNA_view3d_types.h
+++ b/source/blender/makesdna/DNA_view3d_types.h
@@ -373,6 +373,7 @@ typedef struct View3D {
#define V3D_HIDE_HELPLINES (1 << 2)
#define V3D_FLAG_UNUSED_2 (1 << 3) /* cleared */
#define V3D_XR_SESSION_MIRROR (1 << 4)
+#define V3D_XR_SESSION_SURFACE (1 << 5)
#define V3D_FLAG_UNUSED_10 (1 << 10) /* cleared */
#define V3D_SELECT_OUTLINE (1 << 11)
@@ -465,6 +466,8 @@ enum {
#define V3D_FLAG2_UNUSED_13 (1 << 13) /* cleared */
#define V3D_FLAG2_UNUSED_14 (1 << 14) /* cleared */
#define V3D_FLAG2_UNUSED_15 (1 << 15) /* cleared */
+#define V3D_XR_SHOW_CONTROLLERS (1 << 16)
+#define V3D_XR_SHOW_CUSTOM_OVERLAYS (1 << 17)
/** #View3D.gp_flag (short) */
#define V3D_GP_FADE_OBJECTS (1 << 0) /* Fade all non GP objects */
diff --git a/source/blender/makesdna/DNA_xr_types.h b/source/blender/makesdna/DNA_xr_types.h
index a9d427777f7..6f4f7e3e8ae 100644
--- a/source/blender/makesdna/DNA_xr_types.h
+++ b/source/blender/makesdna/DNA_xr_types.h
@@ -42,7 +42,9 @@ typedef struct XrSessionSettings {
/** View3D draw flags (V3D_OFSDRAW_NONE, V3D_OFSDRAW_SHOW_ANNOTATION, ...). */
char draw_flags;
- char _pad2[3];
+ /** Draw style for controller visualization. */
+ char controller_draw_style;
+ char _pad2[2];
/** Clipping distance. */
float clip_start, clip_end;
@@ -61,6 +63,13 @@ typedef enum eXRSessionBasePoseType {
XR_BASE_POSE_CUSTOM = 2,
} eXRSessionBasePoseType;
+typedef enum eXrSessionControllerDrawStyle {
+ XR_CONTROLLER_DRAW_DARK = 0,
+ XR_CONTROLLER_DRAW_LIGHT = 1,
+ XR_CONTROLLER_DRAW_DARK_RAY = 2,
+ XR_CONTROLLER_DRAW_LIGHT_RAY = 3,
+} eXrSessionControllerDrawStyle;
+
/** XR action type. Enum values match those in GHOST_XrActionType enum for consistency. */
typedef enum eXrActionType {
XR_BOOLEAN_INPUT = 1,
diff --git a/source/blender/makesrna/intern/rna_screen.c b/source/blender/makesrna/intern/rna_screen.c
index 912f47ba9aa..a8f28f6091c 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, "XR", 0, "XR", ""},
{0, NULL, 0, NULL, NULL},
};
diff --git a/source/blender/makesrna/intern/rna_xr.c b/source/blender/makesrna/intern/rna_xr.c
index a433a93e403..3705284ca66 100644
--- a/source/blender/makesrna/intern/rna_xr.c
+++ b/source/blender/makesrna/intern/rna_xr.c
@@ -1559,6 +1559,22 @@ static void rna_def_xr_session_settings(BlenderRNA *brna)
{0, NULL, 0, NULL, NULL},
};
+ static const EnumPropertyItem controller_draw_styles[] = {
+ {XR_CONTROLLER_DRAW_DARK, "DARK", 0, "Dark", "Draw dark controller"},
+ {XR_CONTROLLER_DRAW_LIGHT, "LIGHT", 0, "Light", "Draw light controller"},
+ {XR_CONTROLLER_DRAW_DARK_RAY,
+ "DARK_RAY",
+ 0,
+ "Dark + Ray",
+ "Draw dark controller with aiming axis ray"},
+ {XR_CONTROLLER_DRAW_LIGHT_RAY,
+ "LIGHT_RAY",
+ 0,
+ "Light + Ray",
+ "Draw light controller with aiming axis ray"},
+ {0, NULL, 0, NULL, NULL},
+ };
+
srna = RNA_def_struct(brna, "XrSessionSettings", NULL);
RNA_def_struct_ui_text(srna, "XR Session Settings", "");
@@ -1609,6 +1625,29 @@ static void rna_def_xr_session_settings(BlenderRNA *brna)
RNA_def_property_ui_text(prop, "Show Annotation", "Show annotations for this view");
RNA_def_property_update(prop, NC_WM | ND_XR_DATA_CHANGED, NULL);
+ prop = RNA_def_property(srna, "show_selection", PROP_BOOLEAN, PROP_NONE);
+ RNA_def_property_boolean_sdna(prop, NULL, "draw_flags", V3D_OFSDRAW_SHOW_SELECTION);
+ RNA_def_property_ui_text(prop, "Show Selection", "Show selection outlines");
+ RNA_def_property_update(prop, NC_WM | ND_XR_DATA_CHANGED, NULL);
+
+ prop = RNA_def_property(srna, "show_controllers", PROP_BOOLEAN, PROP_NONE);
+ RNA_def_property_boolean_sdna(prop, NULL, "draw_flags", V3D_OFSDRAW_XR_SHOW_CONTROLLERS);
+ RNA_def_property_ui_text(
+ prop, "Show Controllers", "Show VR controllers (requires VR actions for controller poses)");
+ RNA_def_property_update(prop, NC_WM | ND_XR_DATA_CHANGED, NULL);
+
+ prop = RNA_def_property(srna, "show_custom_overlays", PROP_BOOLEAN, PROP_NONE);
+ RNA_def_property_boolean_sdna(prop, NULL, "draw_flags", V3D_OFSDRAW_XR_SHOW_CUSTOM_OVERLAYS);
+ RNA_def_property_ui_text(prop, "Show Custom Overlays", "Show custom VR overlays");
+ RNA_def_property_update(prop, NC_WM | ND_XR_DATA_CHANGED, NULL);
+
+ prop = RNA_def_property(srna, "controller_draw_style", PROP_ENUM, PROP_NONE);
+ RNA_def_property_clear_flag(prop, PROP_ANIMATABLE);
+ RNA_def_property_enum_items(prop, controller_draw_styles);
+ RNA_def_property_ui_text(
+ prop, "Controller Draw Style", "Style to use when drawing VR controllers");
+ RNA_def_property_update(prop, NC_WM | ND_XR_DATA_CHANGED, NULL);
+
prop = RNA_def_property(srna, "clip_start", PROP_FLOAT, PROP_DISTANCE);
RNA_def_property_range(prop, 1e-6f, FLT_MAX);
RNA_def_property_ui_range(prop, 0.001f, FLT_MAX, 10, 3);
diff --git a/source/blender/windowmanager/WM_api.h b/source/blender/windowmanager/WM_api.h
index 531c5aa19bf..68e2c6b38f0 100644
--- a/source/blender/windowmanager/WM_api.h
+++ b/source/blender/windowmanager/WM_api.h
@@ -1013,6 +1013,8 @@ bool WM_xr_session_state_controller_aim_rotation_get(const wmXrData *xr,
unsigned int subaction_idx,
float r_rotation[4]);
+struct ARegionType *WM_xr_surface_controller_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. */
diff --git a/source/blender/windowmanager/xr/intern/wm_xr_draw.c b/source/blender/windowmanager/xr/intern/wm_xr_draw.c
index bbb73fc2007..6b3756c8178 100644
--- a/source/blender/windowmanager/xr/intern/wm_xr_draw.c
+++ b/source/blender/windowmanager/xr/intern/wm_xr_draw.c
@@ -24,12 +24,17 @@
#include <string.h>
+#include "BKE_context.h"
+
#include "BLI_listbase.h"
#include "BLI_math.h"
#include "ED_view3d_offscreen.h"
#include "GHOST_C-api.h"
+#include "GPU_batch_presets.h"
+#include "GPU_immediate.h"
+#include "GPU_matrix.h"
#include "GPU_viewport.h"
@@ -153,6 +158,7 @@ void wm_xr_draw_view(const GHOST_XrDrawViewInfo *draw_view, void *customdata)
winmat,
settings->clip_start,
settings->clip_end,
+ true,
false,
true,
NULL,
@@ -172,3 +178,200 @@ 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);
}
+
+static GPUBatch *wm_xr_controller_model_batch_create(GHOST_XrContextHandle xr_context,
+ const char *subaction_path)
+{
+ GHOST_XrControllerModelData model_data;
+
+ if (!GHOST_XrGetControllerModelData(xr_context, subaction_path, &model_data) ||
+ model_data.count_vertices < 1) {
+ return NULL;
+ }
+
+ GPUVertFormat format = {0};
+ GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
+ GPU_vertformat_attr_add(&format, "nor", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
+
+ GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format);
+ GPU_vertbuf_data_alloc(vbo, model_data.count_vertices);
+ void *vbo_data = GPU_vertbuf_get_data(vbo);
+ memcpy(
+ vbo_data, model_data.vertices, model_data.count_vertices * sizeof(model_data.vertices[0]));
+
+ GPUIndexBuf *ibo = NULL;
+ if (model_data.count_indices > 0 && ((model_data.count_indices % 3) == 0)) {
+ GPUIndexBufBuilder ibo_builder;
+ const unsigned int prim_len = model_data.count_indices / 3;
+ GPU_indexbuf_init(&ibo_builder, GPU_PRIM_TRIS, prim_len, model_data.count_vertices);
+ for (unsigned int i = 0; i < prim_len; ++i) {
+ const uint32_t *idx = &model_data.indices[i * 3];
+ GPU_indexbuf_add_tri_verts(&ibo_builder, idx[0], idx[1], idx[2]);
+ }
+ ibo = GPU_indexbuf_build(&ibo_builder);
+ }
+
+ return GPU_batch_create_ex(GPU_PRIM_TRIS, vbo, ibo, GPU_BATCH_OWNS_VBO | GPU_BATCH_OWNS_INDEX);
+}
+
+static void wm_xr_controller_model_draw(const XrSessionSettings *settings,
+ GHOST_XrContextHandle xr_context,
+ wmXrSessionState *state)
+{
+ GHOST_XrControllerModelData model_data;
+
+ float color[4];
+ switch (settings->controller_draw_style) {
+ case XR_CONTROLLER_DRAW_DARK:
+ case XR_CONTROLLER_DRAW_DARK_RAY:
+ color[0] = color[1] = color[2] = 0.0f, color[3] = 0.4f;
+ break;
+ case XR_CONTROLLER_DRAW_LIGHT:
+ case XR_CONTROLLER_DRAW_LIGHT_RAY:
+ color[0] = 0.422f, color[1] = 0.438f, color[2] = 0.446f, color[3] = 0.4f;
+ break;
+ }
+
+ GPU_depth_test(GPU_DEPTH_NONE);
+ GPU_blend(GPU_BLEND_ALPHA);
+
+ LISTBASE_FOREACH (wmXrController *, controller, &state->controllers) {
+ GPUBatch *model = controller->model;
+ if (!model) {
+ model = controller->model = wm_xr_controller_model_batch_create(xr_context,
+ controller->subaction_path);
+ }
+
+ if (model &&
+ GHOST_XrGetControllerModelData(xr_context, controller->subaction_path, &model_data) &&
+ model_data.count_components > 0) {
+ GPU_batch_program_set_builtin(model, GPU_SHADER_3D_UNIFORM_COLOR);
+ GPU_batch_uniform_4fv(model, "color", color);
+
+ GPU_matrix_push();
+ GPU_matrix_mul(controller->grip_mat);
+ for (unsigned int component_idx = 0; component_idx < model_data.count_components;
+ ++component_idx) {
+ const GHOST_XrControllerModelComponent *component = &model_data.components[component_idx];
+ GPU_matrix_push();
+ GPU_matrix_mul(component->transform);
+ GPU_batch_draw_range(model,
+ model->elem ? component->index_offset : component->vertex_offset,
+ model->elem ? component->index_count : component->vertex_count);
+ GPU_matrix_pop();
+ }
+ GPU_matrix_pop();
+ }
+ else {
+ /* Fallback. */
+ const float scale = 0.05f;
+ GPUBatch *sphere = GPU_batch_preset_sphere(2);
+ GPU_batch_program_set_builtin(sphere, GPU_SHADER_3D_UNIFORM_COLOR);
+ GPU_batch_uniform_4fv(sphere, "color", color);
+
+ GPU_matrix_push();
+ GPU_matrix_mul(controller->grip_mat);
+ GPU_matrix_scale_1f(scale);
+ GPU_batch_draw(sphere);
+ GPU_matrix_pop();
+ }
+ }
+}
+
+static void wm_xr_controller_aim_draw(const XrSessionSettings *settings, wmXrSessionState *state)
+{
+ bool draw_ray;
+ switch (settings->controller_draw_style) {
+ case XR_CONTROLLER_DRAW_DARK:
+ case XR_CONTROLLER_DRAW_LIGHT:
+ draw_ray = false;
+ break;
+ case XR_CONTROLLER_DRAW_DARK_RAY:
+ case XR_CONTROLLER_DRAW_LIGHT_RAY:
+ draw_ray = true;
+ break;
+ }
+
+ GPUVertFormat *format = immVertexFormat();
+ uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
+ uint col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
+ immBindBuiltinProgram(GPU_SHADER_3D_POLYLINE_FLAT_COLOR);
+
+ float viewport[4];
+ GPU_viewport_size_get_f(viewport);
+ immUniform2fv("viewportSize", &viewport[2]);
+
+ immUniform1f("lineWidth", 3.0f * U.pixelsize);
+
+ if (draw_ray) {
+ const uchar color[4] = {89, 89, 255, 127};
+ const float scale = settings->clip_end;
+ float ray[3];
+
+ GPU_depth_test(GPU_DEPTH_LESS_EQUAL);
+ GPU_blend(GPU_BLEND_ALPHA);
+
+ immBegin(GPU_PRIM_LINES, (uint)BLI_listbase_count(&state->controllers) * 2);
+
+ LISTBASE_FOREACH (wmXrController *, controller, &state->controllers) {
+ const float(*mat)[4] = controller->aim_mat;
+ madd_v3_v3v3fl(ray, mat[3], mat[2], -scale);
+
+ immAttrSkip(col);
+ immVertex3fv(pos, mat[3]);
+ immAttr4ubv(col, color);
+ immVertex3fv(pos, ray);
+ }
+
+ immEnd();
+ }
+ else {
+ const uchar r[4] = {255, 51, 82, 255};
+ const uchar g[4] = {139, 220, 0, 255};
+ const uchar b[4] = {40, 144, 255, 255};
+ const float scale = 0.01f;
+ float x_axis[3], y_axis[3], z_axis[3];
+
+ GPU_depth_test(GPU_DEPTH_NONE);
+ GPU_blend(GPU_BLEND_NONE);
+
+ immBegin(GPU_PRIM_LINES, (uint)BLI_listbase_count(&state->controllers) * 6);
+
+ LISTBASE_FOREACH (wmXrController *, controller, &state->controllers) {
+ const float(*mat)[4] = controller->aim_mat;
+ madd_v3_v3v3fl(x_axis, mat[3], mat[0], scale);
+ madd_v3_v3v3fl(y_axis, mat[3], mat[1], scale);
+ madd_v3_v3v3fl(z_axis, mat[3], mat[2], scale);
+
+ immAttrSkip(col);
+ immVertex3fv(pos, mat[3]);
+ immAttr4ubv(col, r);
+ immVertex3fv(pos, x_axis);
+
+ immAttrSkip(col);
+ immVertex3fv(pos, mat[3]);
+ immAttr4ubv(col, g);
+ immVertex3fv(pos, y_axis);
+
+ immAttrSkip(col);
+ immVertex3fv(pos, mat[3]);
+ immAttr4ubv(col, b);
+ immVertex3fv(pos, z_axis);
+ }
+
+ immEnd();
+ }
+
+ immUnbindProgram();
+}
+
+void wm_xr_draw_controllers(const bContext *UNUSED(C), ARegion *UNUSED(region), void *customdata)
+{
+ wmXrData *xr = customdata;
+ const XrSessionSettings *settings = &xr->session_settings;
+ GHOST_XrContextHandle xr_context = xr->runtime->context;
+ wmXrSessionState *state = &xr->runtime->session_state;
+
+ wm_xr_controller_model_draw(settings, xr_context, state);
+ wm_xr_controller_aim_draw(settings, state);
+}
diff --git a/source/blender/windowmanager/xr/intern/wm_xr_intern.h b/source/blender/windowmanager/xr/intern/wm_xr_intern.h
index 8fb5424b8c2..2cd0ba5c056 100644
--- a/source/blender/windowmanager/xr/intern/wm_xr_intern.h
+++ b/source/blender/windowmanager/xr/intern/wm_xr_intern.h
@@ -88,6 +88,11 @@ typedef struct wmXrViewportPair {
typedef struct {
/** Off-screen buffers/viewports for each view. */
ListBase viewports; /* #wmXrViewportPair */
+
+ /** Dummy region type for controller draw callback. */
+ struct ARegionType *controller_art;
+ /** Controller draw callback handle. */
+ void *controller_draw_handle;
} wmXrSurfaceData;
typedef struct wmXrDrawData {
@@ -114,12 +119,16 @@ typedef struct wmXrController {
/input/trigger/value, interaction_path = /user/hand/left/input/trigger/value).
*/
char subaction_path[64];
- /* Pose (in world space) that represents the user's hand when holding the controller.*/
+
+ /** Pose (in world space) that represents the user's hand when holding the controller. */
GHOST_XrPose grip_pose;
float grip_mat[4][4];
- /* Pose (in world space) that represents the controller's aiming source. */
+ /** Pose (in world space) that represents the controller's aiming source. */
GHOST_XrPose aim_pose;
float aim_mat[4][4];
+
+ /** Controller model. */
+ struct GPUBatch *model;
} wmXrController;
typedef struct wmXrAction {
@@ -207,3 +216,4 @@ void wm_xr_session_controller_data_clear(wmXrSessionState *state);
void wm_xr_pose_to_mat(const GHOST_XrPose *pose, float r_mat[4][4]);
void wm_xr_pose_to_imat(const GHOST_XrPose *pose, float r_imat[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 88bc5c45c91..9f53db1347c 100644
--- a/source/blender/windowmanager/xr/intern/wm_xr_session.c
+++ b/source/blender/windowmanager/xr/intern/wm_xr_session.c
@@ -24,6 +24,7 @@
#include "BKE_idprop.h"
#include "BKE_main.h"
#include "BKE_scene.h"
+#include "BKE_screen.h"
#include "BLI_listbase.h"
#include "BLI_math.h"
@@ -36,9 +37,11 @@
#include "DRW_engine.h"
#include "ED_screen.h"
+#include "ED_space_api.h"
#include "GHOST_C-api.h"
+#include "GPU_batch.h"
#include "GPU_viewport.h"
#include "MEM_guardedalloc.h"
@@ -72,7 +75,15 @@ static void wm_xr_session_create_cb(void)
static void wm_xr_session_controller_data_free(wmXrSessionState *state)
{
- BLI_freelistN(&state->controllers);
+ ListBase *lb = &state->controllers;
+ wmXrController *c;
+
+ while ((c = BLI_pophead(lb))) {
+ if (c->model) {
+ GPU_batch_discard(c->model);
+ }
+ BLI_freelinkN(lb, c);
+ }
}
void wm_xr_session_data_free(wmXrSessionState *state)
@@ -529,6 +540,7 @@ static void wm_xr_session_controller_pose_calc(const GHOST_XrPose *raw_pose,
static void wm_xr_session_controller_data_update(const XrSessionSettings *settings,
const wmXrAction *grip_action,
const wmXrAction *aim_action,
+ GHOST_XrContextHandle xr_context,
wmXrSessionState *state)
{
BLI_assert(grip_action->count_subaction_paths == aim_action->count_subaction_paths);
@@ -560,6 +572,16 @@ static void wm_xr_session_controller_data_update(const XrSessionSettings *settin
base_mat,
&controller->aim_pose,
controller->aim_mat);
+
+ if (!controller->model) {
+ /* Notify GHOST to load/continue loading the controller model data. This can be called more
+ * than once since the model may not be available from the runtime yet. The batch itself will
+ * be created in wm_xr_draw_controllers(). */
+ GHOST_XrLoadControllerModel(xr_context, controller->subaction_path);
+ }
+ else {
+ GHOST_XrUpdateControllerModelComponents(xr_context, controller->subaction_path);
+ }
}
}
@@ -1089,6 +1111,7 @@ void wm_xr_session_actions_update(wmWindowManager *wm)
wm_xr_session_controller_data_update(&xr->session_settings,
active_action_set->controller_grip_action,
active_action_set->controller_aim_action,
+ xr_context,
state);
}
@@ -1125,11 +1148,33 @@ void wm_xr_session_controller_data_populate(const wmXrAction *grip_action,
BLI_addtail(controllers, controller);
}
+
+ /* 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->controller_art) {
+ surface_data->controller_draw_handle = ED_region_draw_cb_activate(
+ surface_data->controller_art, wm_xr_draw_controllers, xr, REGION_DRAW_POST_VIEW);
+ }
+ }
+ }
}
void wm_xr_session_controller_data_clear(wmXrSessionState *state)
{
wm_xr_session_controller_data_free(state);
+
+ /* Deactivate draw callback. */
+ if (g_xr_surface) {
+ wmXrSurfaceData *surface_data = g_xr_surface->customdata;
+ if (surface_data && surface_data->controller_draw_handle) {
+ if (surface_data->controller_art) {
+ ED_region_draw_cb_exit(surface_data->controller_art, surface_data->controller_draw_handle);
+ }
+ surface_data->controller_draw_handle = NULL;
+ }
+ }
}
/** \} */ /* XR-Session Actions */
@@ -1255,6 +1300,11 @@ static void wm_xr_session_surface_free_data(wmSurface *surface)
BLI_freelinkN(lb, vp);
}
+ if (data->controller_art) {
+ BLI_freelistN(&data->controller_art->drawcalls);
+ MEM_freeN(data->controller_art);
+ }
+
MEM_freeN(surface->customdata);
g_xr_surface = NULL;
@@ -1269,6 +1319,7 @@ static wmSurface *wm_xr_session_surface_create(void)
wmSurface *surface = MEM_callocN(sizeof(*surface), __func__);
wmXrSurfaceData *data = MEM_callocN(sizeof(*data), "XrSurfaceData");
+ data->controller_art = MEM_callocN(sizeof(*(data->controller_art)), "XrControllerRegionType");
surface->draw = wm_xr_session_surface_draw;
surface->free_data = wm_xr_session_surface_free_data;
@@ -1278,6 +1329,7 @@ static wmSurface *wm_xr_session_surface_create(void)
surface->ghost_ctx = DRW_xr_opengl_context_get();
surface->gpu_ctx = DRW_xr_gpu_context_get();
+ data->controller_art->regionid = RGN_TYPE_XR;
surface->customdata = data;
g_xr_surface = surface;
@@ -1311,4 +1363,14 @@ void wm_xr_session_gpu_binding_context_destroy(GHOST_ContextHandle UNUSED(contex
WM_main_add_notifier(NC_WM | ND_XR_DATA_CHANGED, NULL);
}
+ARegionType *WM_xr_surface_controller_region_type_get(void)
+{
+ if (g_xr_surface) {
+ wmXrSurfaceData *data = g_xr_surface->customdata;
+ return data->controller_art;
+ }
+
+ return NULL;
+}
+
/** \} */ /* XR-Session Surface */