openxr runtime Monado 源码解析 源码分析:源码编译 准备工作说明 hello_xr解读

monado系列文章索引汇总:
openxr runtime Monado 源码解析 源码分析:源码编译 准备工作说明 hello_xr解读
openxr runtime Monado 源码解析 源码分析:整体介绍 模块架构 模块作用 进程 线程模型 整体流程
openxr runtime Monado 源码解析 源码分析:CreateInstance流程(设备系统和合成器系统)Compositor comp_main client compositor
openxr runtime Monado 源码解析 源码分析:Prober设备发现和管理 system device HMD target instance
openxr runtime Monado 源码解析 源码分析:InitializeSession client native multi_compositor client_compositor
openxr runtime Monado 源码解析 源码分析:CreateSwapchain 画布 HardwareBuffer共享纹理 渲染线程 xrEndeFrame comp_renderer

目录

说明

用到的三个源码工程和功能概述

OpenXR-SDK-Source

openxr-android-broker

monado

三个代码库间的关系

源码编译和运行效果

源码编译

运行顺序和效果

hello_xr渲染部分源码分析

hello_xr整体架构

graphicsplugin_opengles模块

App侧(client侧)渲染结果如何被Runtime合成的

附录


说明

1 本文是系列文章的第一篇

2 源码阅读基于安卓平台

3 以Monado合成器系统(Compositor)为主线的源码分析

4 此篇介绍整个分析过程中用到的开源工程,每个工程的作用

5 重点分析hello_xr源码

用到的三个源码工程和功能概述

OpenXR-SDK-Source

openxr-android-broker

monado

三个代码库间的关系

OpenXR文档给出了Application Loader Runtime三者的关系,如下图:

原图参见:https://registry.khronos.org/OpenXR/specs/1.0/loader.html#Overview

上图是抽象的模块关系表达,对应到三个源码库的关系如下:

源码编译和运行效果

源码编译

每个工程都可以编译成独立的APK

源码编译较简单,不再赘述,如果有疑惑,可以留言,我抽空补上一篇文章。

推荐编译文章(未经作者授权):https://www.jianshu.com/p/6653507de3c6

运行顺序和效果

依次打开:

(1)Monado XR(in-process下无界面)

(2)OpenXR Runtime Broker,界面如下

(3)Hello XR(OpenGL ES),也可以是Vulkan版本,后续基于GLES分析,效果如下:

hello_xr渲染部分源码分析

本文忽略loader模块,重点讨论渲染方面的内容。Vulkan太啰嗦,为了更容易搞明白怎么在App侧(Client端)渲染,以及与Monado互动过程,本节以OpenGL ES部分为分析对象。分为以下几个部分:hello_xr整体架构;OpenXrProgram模块;GraphicisPlugin_OpenGLES模块。

hello_xr重点:app(client绘制的内容,如何被runtime合成的)

hello_xr整体架构

整体架构如下:

main主进程:安卓NativeActivity主入口,用于加载OpenXR Runtime,创建平台相关对象(platformplugin_android),图形渲染库(graphicsplugin_opengles)。

platformplugin_android:从源码里可以看出,demo支持win32 xlib xcb等平台,本文研究安卓平台,该模块提供几个重要字段:

    • XR_KHR_android_create_instance扩展
    • FormFactor: 'Hmd'
    • ViewConfiguration: 'Stereo'
    • EnvironmentBlendMode: 'Opaque'
    • AppSpace: 'Local'

graphicsplugin_opengles:根据运行平台不同,Runtime暴露给App侧的渲染模块可以是D3D,Vulkan,OpenGL,OpenGL ES。Vulkan相对繁琐,本文以OpenGL ES作为梳理对象,以此整理App侧(client侧)渲染,渲染结果与Runtime合成关系。

OpenXR Loader:标准加载器实现,将来扩展Runtime功能时可能用到,暂不研究。

OpenXR Runtime:这里特指Monado开源引擎,后续文章会介绍。

graphicsplugin_opengles模块

这个模块主要是完成3D应用场景的绘制,简单一点用API裸写3D渲染,成熟做法是接入主流游戏引擎,如Unity UE Cocos等。熟悉OpenGL ES的同学打开graphicsplugin_opengles.cpp文件,可以清晰地看到几个关键模块:

(1)利用三方库进行渲染环境创建ksGpuWindow_Create()

EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY)
eglInitialize(display, major, minor)
EGLConfig eglGetConfigs()
EGLContext eglCreateContext(d, c, NO_CONTEXT)
EGLSurface eglCreatePbufferSurface(d, c)
eglMakeCurrent(display, surface, context)

重点:PBuffer,创建的是离屏渲染,因为合成展示在Runtime里完成。

(2)GLES资源创建InitializeResources()

glGenFramebuffers # 创建FBO
glCreateProgram   # 创建立方体绘制需要的shader
glGenBuffers      # 创建立方体VBO和IBO

(3)3D场景绘制RenderView(layerView, swapchainImage)

glBindFramebuffer()
glViewport()
glFrontFace(GL_CW)
uint32_t colorTexture = reinterpret_cast<const XrSwapchainImageOpenGLESKHR*>(swapchainImage)->image;
glFramebufferTexture2D(colorTexture| DEPTH)
glClearColor()
glClear()
glUseProgram(m_program)
矩阵计算:fov pose(position orientation) scale,来自runtime模块
glBindVertexArray(m_vao)
// Render each cube
glDrawElements()

重点:可以看出App侧的Framebuffer的COLOR_ATTACHMENT0和DEPTH_ATTACHMENT来自Runtime的对象:SwapchainImage,也就是说客户端的画布是runtime创建,并通过纹理共享的,后续文章会详细介绍。

App侧(client侧)渲染结果如何被Runtime合成的

这块实现比较复杂,涉及到Runtime的合成器系统(compositor),可以先理解几个核心步骤,细节会在后续博文解读:

(1)Runtime的主合成器(comp_compositor)创建Vulkan渲染环境

(2)Runtime负责衔接Client端合成器(comp_multi_compositor)创建一个共享VK内存

(3)多客户端合成器系统(multi_system_compositor)根据客户端的渲染环境,如GLES GL VK等,用步骤(2)创建的共享VK内存,创建新的纹理对象,如EGLImage,再创建glTexture,然后通过SwapchainImage接口返回给App层使用。

这里一个技术要点是:基于HardwareBuffer的多线程纹理共享技术,具体参加附录。

附录

基于 HardwareBuffer 实现 Android 多进程渲染

https://robot9.me/hardwarebuffer-multi-process-rendering/

关键词 OpenXR Runtime Monado Compositor Vulkan OpenGL ES Swapchain hello_xr VR AR

猜你喜欢

转载自blog.csdn.net/xuer_07/article/details/127953477
今日推荐