OpenXR SDK新增拓展接口

本文详细介绍了如何在OpenXRruntime中新增接口,包括定义拓展、声明接口、函数指针的定义和赋值,以及在SDK中注册和调用拓展接口。以XR_EXT_audio为例,展示了如何实现在运行时添加和使用自定义功能。
摘要由CSDN通过智能技术生成

Khronos给出的官方拓展接口说明:OpenXR™ Documentation and Extensions: Procedures and Conventions

拓展的实现需要以下几步:

目录

1:在runtime中新增接口

1.1 定义拓展,以及拓展接口的声明

1.2 函数指针的定义

1.3 函数指针的赋值

1.4 oxr_函数的具体实现

2. sdk中如何调用拓展接口?

2.1 注册拓展

2.2 调用拓展接口

3. 接口调试


1:在runtime中新增接口

1.1 定义拓展,以及拓展接口的声明

openxr.h文件中进行函数的声明

#define XR_EXT_audio 1  
#define XR_EXT_audio_SPEC_VERSION 1
#define XR_EXT_AUDIO_EXTENSION_NAME "XR_EXT_audio"#ifndef XR_NO_PROTOTYPES

#ifdef XR_EXTENSION_PROTOTYPES
XRAPI_ATTR XrResult XRAPI_CALL
                           xrDistanceAttention(bool enable, float max, float min, int radius);
#endif /* XR_EXTENSION_PROTOTYPES */
#endif /* !XR_NO_PROTOTYPES */

1.2 函数指针的定义

  函数指针必须以PFN_开头,这个是openxr规范之一

#define XR_EXT_audio 1
#define XR_EXT_audio_SPEC_VERSION 1
#define XR_EXT_AUDIO_EXTENSION_NAME "XR_EXT_audio"
typedef XrResult (XRAPI_PTR *PFN_xrDistanceAttention)(bool enable, uint32_t max, uint32_t min, uint32_t radius);

#ifndef XR_NO_PROTOTYPES
#ifdef XR_EXTENSION_PROTOTYPES
XRAPI_ATTR XrResult XRAPI_CALL
                          xrDistanceAttention(bool enable, float max, float min, int radius);
#endif /* XR_EXTENSION_PROTOTYPES */
#endif /* !XR_NO_PROTOTYPES */

1.3 函数指针的赋值

oxr_api_funcs.h中oxr_xrGetInstanceProcAddr函数中在Instance实例化后,会调用handle_non_null,这时对函数进行赋值,即会对1.1中的函数前加个oxr_

static XrResult
handle_non_null(struct oxr_instance *inst, struct oxr_logger *log, const char *name, PFN_xrVoidFunction *out_function)
{

...

ENTRY(xrDistanceAttention);

...

}

ENTRY函数如下:
#define ENTRY(funcName) \
do { \
if (strcmp(name, #funcName) == 0) { \
PFN_##funcName ret = &oxr_##funcName; \
*out_function = (PFN_xrVoidFunction)(ret); \
return XR_SUCCESS; \
} \
} while (false)

1.4 oxr_函数的具体实现

XRAPI_ATTR XrResult XRAPI_CALL
oxr_xrDistanceAttention(bool enable, uint32_t max, uint32_t min, uint32_t radius) {
__android_log_print(ANDROID_LOG_DEBUG, "shanshan","add oxr_xrDistanceAttention");
}

2. sdk中如何调用拓展接口?

2.1 注册拓展

xr.xml中新增拓展

<!-- commands for XR_EXT_audio -->
<command successcodes="XR_SUCCESS" errorcodes="XR_ERROR_FUNCTION_UNSUPPORTED,XR_ERROR_VALIDATION_FAILURE">
    <proto><type>XrResult</type> <name>xrDistanceAttention</name></proto>
    <param><type>XrBool32</type> <name>enable</name></param>
    <param><type>uint32_t</type> <name>max</name></param>
    <param><type>uint32_t</type> <name>min</name></param>
    <param><type>uint32_t</type> <name>radius</name></param>
</command>

--- supported="openxr"代表启动拓展,supported="disabled"代表禁用拓展,number号要注意一下,不能和别的相同

<extension name="XR_EXT_audio" number="498" type="instance" supported="openxr">
   <require>
        <enum value="1" name="XR_EXT_audio_SPEC_VERSION"/>
        <enum value="&quot;XR_EXT_audio&quot;" name="XR_EXT_AUDIO_EXTENSION_NAME"/>

        <command name="xrDistanceAttention"/>
    </require>
</extension>

2.2 调用拓展接口

void InitializeSystem() override {

...

PFN_xrDistanceAttention pfnXrDistanceAttention = nullptr;-----PFN_xrDistanceAttention 这个定义会在编译出来的openxr.h文件中能看到,与runtime中的openxr.h一致
__android_log_print(ANDROID_LOG_DEBUG, "shanshan", "SDK begin pfnXrDistanceAttention");
xrGetInstanceProcAddr(m_instance, "xrDistanceAttention",reinterpret_cast<PFN_xrVoidFunction*>(&pfnXrDistanceAttention));---拓展通过xrGetInstanceProcAddr调用
pfnXrDistanceAttention(true, 1, 1, 1);---调用runtime中的接口
__android_log_print(ANDROID_LOG_DEBUG, "shanshan", "SDK end pfnXrDistanceAttention");

...

}

3. 接口调试

分别安装runtime应用,system broker应用,以及helloxr应用,然后启动system broker应用,选择monado runtime, 然后启动helloxr应用,由于,我们是在应用初始化的时候调用了接口,所以在helloxr应用启动后,会自动调用我们的拓展接口,log输出如下:

07-29 08:57:34.975 9289 9310 D shanshan: SDK begin pfnXrDistanceAttention
07-29 08:57:34.975 9289 9310 D shanshan: add oxr_xrDistanceAttention
07-29 08:57:34.975 9289 9310 D shanshan: SDK end pfnXrDistanceAttention

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值