美文网首页
openxr的例子helloxr

openxr的例子helloxr

作者: 赛非斯 | 来源:发表于2022-04-26 20:38 被阅读0次
    • openxr的生命周期:

      image.png
      官方代码:
      https://github.com/KhronosGroup/OpenXR-SDK-Source.git
    • 用androidstudio 打开项目hello_xr ,NativeActivity 就是此android项目的主界面,它加载了hello_xr 库
      入口函数就是src/tests/hello_xr/main.cpp 的 void android_main(struct android_app* app)


      image.png
    • 参照上图的openxr的生命周期,有下面代码中几个重要的环节

    void  android_main(struct android_app* app) {
         // Create platform-specific implementation. 指定平台插件,有android windows等
            std::shared_ptr<IPlatformPlugin> platformPlugin = CreatePlatformPlugin(options, data);
            // Create graphics API implementation.  指定图形库插件 有vulkan opengl等
            std::shared_ptr<IGraphicsPlugin> graphicsPlugin = CreateGraphicsPlugin(options, platformPlugin);
    
            // Initialize the OpenXR program.  初始化 OpenXR 程序
            std::shared_ptr<IOpenXrProgram> program = CreateOpenXrProgram(options, platformPlugin, graphicsPlugin);
    
    //初始化loader
    initializeLoader((const XrLoaderInitInfoBaseHeaderKHR*)&loaderInitInfoAndroid);
    //program 就是 src/tests/hello_xr/openxr_program.cpp
    //初始化XrInstanceCreateInfo
           program->CreateInstance();
    //xrGetSystem 、InitializeDevice 初始化设备
            program->InitializeSystem();
    //xrCreateSession  初始化session
            program->InitializeSession();
    //创建交换链
            program->CreateSwapchains();
    //一切准备就绪之后进入循环
     while (app->destroyRequested == 0) {
      //对应流程图里的xrPollEvents
          program->PollEvents(&exitRenderLoop, &requestRestart);
    
     //处理图像帧
      program->PollActions();
      program->RenderFrame();
    }
    }
    
    • PollEvents 相当于接收事件处理事件的逻辑,代码路径
      src/tests/hello_xr/openxr_program.cpp
    void PollEvents(bool* exitRenderLoop, bool* requestRestart) override {
    //这里对应了流程图里面的XRSessionState
          case XR_TYPE_EVENT_DATA_SESSION_STATE_CHANGED: {
                        auto sessionStateChangedEvent = *reinterpret_cast<const XrEventDataSessionStateChanged*>(event);
                        HandleSessionStateChangedEvent(sessionStateChangedEvent, exitRenderLoop, requestRestart);
                        break;
                    }
    }
    //这里对应了xrBeginSession 和 xrEndSession
    void HandleSessionStateChangedEvent(const XrEventDataSessionStateChanged& stateChangedEvent, bool* exitRenderLoop,
                                            bool* requestRestart) {
    switch (m_sessionState) {
                case XR_SESSION_STATE_READY: {
                    CHECK_XRCMD(xrBeginSession(m_session, &sessionBeginInfo));
                    m_sessionRunning = true;
                    break;
                }
                case XR_SESSION_STATE_STOPPING: {
                    CHECK(m_session != XR_NULL_HANDLE);
                    m_sessionRunning = false;
                    CHECK_XRCMD(xrEndSession(m_session))
                    break;
                }
    
    }
    
    • RenderFrame 渲染帧
    void RenderFrame() override {
    //等待帧
    xrWaitFrame
    //开始处理帧
    xrBeginFrame
     std::vector<XrCompositionLayerBaseHeader*> layers;
            XrCompositionLayerProjection layer{XR_TYPE_COMPOSITION_LAYER_PROJECTION};
            std::vector<XrCompositionLayerProjectionView> projectionLayerViews;
            if (frameState.shouldRender == XR_TRUE) {
              //渲染图层
                if (RenderLayer(frameState.predictedDisplayTime, projectionLayerViews, layer)) {
                    layers.push_back(reinterpret_cast<XrCompositionLayerBaseHeader*>(&layer));
                }
            }
    //结束一帧的处理
    xrEndFrame
    }
    
    • RenderLayer 感觉是图形处理的核心逻辑,这里用Swapchain处理,对应流程图的xrAcquireSwapchainImage 、xrWaitSwapchainImage、xrReleaseSwapchainImage 逻辑
    bool RenderLayer(XrTime predictedDisplayTime, std::vector<XrCompositionLayerProjectionView>& projectionLayerViews,
                         XrCompositionLayerProjection& layer) {
    //视图位置
    XrViewLocateInfo 
    res = xrLocateViews(m_session, &viewLocateInfo, &viewState, viewCapacityInput, &viewCountOutput, m_views.data());
    res = xrLocateSpace(m_input.handSpace[hand], m_appSpace, predictedDisplayTime, &spaceLocation);
    //将视图渲染到交换链图像的适当部分
     for (uint32_t i = 0; i < viewCountOutput; i++) {
                // Each view has a separate swapchain which is acquired, rendered to, and released.
                const Swapchain viewSwapchain = m_swapchains[i];
    
     CHECK_XRCMD(xrAcquireSwapchainImage(viewSwapchain.handle, &acquireInfo, &swapchainImageIndex));
    CHECK_XRCMD(xrWaitSwapchainImage(viewSwapchain.handle, &waitInfo));
    //这里将交换链中的图像送入图形库渲染 m_graphicsPlugin->RenderView,这里渲染用的src/tests/hello_xr/graphicsplugin_vulkan.cpp 的 RenderView 方法
    const XrSwapchainImageBaseHeader* const swapchainImage = m_swapchainImages[viewSwapchain.handle][swapchainImageIndex];
                m_graphicsPlugin->RenderView(projectionLayerViews[i], swapchainImage, m_colorSwapchainFormat, cubes);
    
     CHECK_XRCMD(xrReleaseSwapchainImage(viewSwapchain.handle, &releaseInfo));
    }
    }
    

    相关文章

      网友评论

          本文标题:openxr的例子helloxr

          本文链接:https://www.haomeiwen.com/subject/rprhyrtx.html