OpenXR SDK新增拓展接口

网上很多家都有自己的拓展,比如pico家

手柄 | PICO 开发者平台

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","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>--拓展函数入参1
    <param><type>uint32_t</type> <name>max</name></param>--拓展函数入参2
    <param><type>uint32_t</type> <name>min</name></param>--拓展函数入参3
    <param><type>uint32_t</type> <name>radius</name></param>--拓展函数入参4
</command>

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

<extension name="XR_EXT_audio" number="498" type="instance" supported="openxr">------拓展名在runtime中的openxr.h中定义好了,#define XR_EXT_audio 1
   <require>
        <enum value="1" name="XR_EXT_audio_SPEC_VERSION"/>---在runtime中的openxr.h中定义好了,#define XR_EXT_audio_SPEC_VERSION 1
        <enum value="&quot;XR_EXT_audio&quot;" name="XR_EXT_AUDIO_EXTENSION_NAME"/>---runtime中openxr.h定义好了,#define XR_EXT_AUDIO_EXTENSION_NAME "XR_EXT_audio"

        <command name="xrDistanceAttention"/>---拓展函数名,如果有多个,此处就有多行command
    </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----sdk中的log,调用开始
07-29 08:57:34.975 9289 9310 D shanshan: shanshan add oxr_xrDistanceAttention------这个是runtime中的log
07-29 08:57:34.975 9289 9310 D shanshan: SDK end pfnXrDistanceAttention----sdk中的log,调用结束

猜你喜欢

转载自blog.csdn.net/weixin_41028555/article/details/132297334