openxr的例子helloxr

2022-04-26  本文已影响0人  赛非斯
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();
}
}
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;
            }

}
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
}
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));
}
}
上一篇下一篇

猜你喜欢

热点阅读