@@ -617,6 +617,8 @@ const vrb::Vector kAverageHeight(0.0f, 1.7f, 0.0f);
617617const vrb::Vector kAverageOculusHeight (0 .0f , 1 .65f , 0 .0f );
618618
619619struct DeviceDelegateOculusVR ::State {
620+ const static uint32_t EXTERNAL_SURFACE_BUFFER_SIZE = 2 ;
621+
620622 struct ControllerState {
621623 const int32_t index;
622624 const ElbowModel::HandEnum hand;
@@ -645,6 +647,7 @@ struct DeviceDelegateOculusVR::State {
645647 ovrJava java = {};
646648 ovrMobile* ovr = nullptr ;
647649 OculusEyeSwapChainPtr eyeSwapChains[VRAPI_EYE_COUNT];
650+ ovrTextureSwapChain* eyeSurfaceSwapChain[EXTERNAL_SURFACE_BUFFER_SIZE];
648651 OculusLayerCubePtr cubeLayer;
649652 OculusLayerEquirectPtr equirectLayer;
650653 std::vector<OculusLayerPtr> uiLayers;
@@ -707,6 +710,9 @@ struct DeviceDelegateOculusVR::State {
707710 exit (status);
708711 return ;
709712 }
713+ for (int i = 0 ; i < EXTERNAL_SURFACE_BUFFER_SIZE; ++i) {
714+ eyeSurfaceSwapChain[i] = nullptr ;
715+ }
710716 initialized = true ;
711717 SetRenderSize (device::RenderMode::StandAlone);
712718
@@ -1128,6 +1134,19 @@ DeviceDelegateOculusVR::SetRenderMode(const device::RenderMode aMode) {
11281134 m.eyeSwapChains [i]->Init (render, m.renderMode , m.renderWidth , m.renderHeight );
11291135 }
11301136
1137+ if (aMode == device::RenderMode::Immersive) {
1138+ uint32_t width, height;
1139+ m.GetImmersiveRenderSize (width, height);
1140+ for (int i = 0 ; i < DeviceDelegateOculusVR::State::EXTERNAL_SURFACE_BUFFER_SIZE; ++i) {
1141+ if (!m.eyeSurfaceSwapChain [i]) {
1142+ m.eyeSurfaceSwapChain [i] = vrapi_CreateAndroidSurfaceSwapChain (width, height);
1143+ auto surfaceOut = vrapi_GetTextureSwapChainAndroidSurface (m.eyeSurfaceSwapChain [i]);
1144+ surfaceOut = m.java .Env ->NewGlobalRef (surfaceOut);
1145+ VRBrowser::SetExternalVRSurface (i, surfaceOut);
1146+ }
1147+ }
1148+ }
1149+
11311150 m.UpdateTrackingMode ();
11321151 m.UpdateFoveatedLevel ();
11331152 m.UpdateDisplayRefreshRate ();
@@ -1340,8 +1359,6 @@ DeviceDelegateOculusVR::StartFrame() {
13401359 ovrMatrix4f matrix = vrapi_GetTransformFromPose (&m.predictedTracking .HeadPose .Pose );
13411360 vrb::Matrix head = vrb::Matrix::FromRowMajor (matrix.M [0 ]);
13421361
1343-
1344-
13451362 if (m.renderMode == device::RenderMode::StandAlone) {
13461363 head.TranslateInPlace (kAverageHeight );
13471364 }
@@ -1359,6 +1376,7 @@ DeviceDelegateOculusVR::StartFrame() {
13591376 caps |= device::PositionEmulated;
13601377 }
13611378 m.immersiveDisplay ->SetCapabilityFlags (caps);
1379+ m.immersiveDisplay ->SetExternalSurfaceInfo (State::EXTERNAL_SURFACE_BUFFER_SIZE, externalSurfaceId);
13621380 }
13631381
13641382 int lastReorientCount = m.reorientCount ;
@@ -1458,13 +1476,33 @@ DeviceDelegateOculusVR::EndFrame(const bool aDiscard) {
14581476 projection.HeadPose = m.predictedTracking .HeadPose ;
14591477 projection.Header .SrcBlend = VRAPI_FRAME_LAYER_BLEND_ONE;
14601478 projection.Header .DstBlend = VRAPI_FRAME_LAYER_BLEND_ONE_MINUS_SRC_ALPHA;
1461- for (int i = 0 ; i < VRAPI_FRAME_LAYER_EYE_MAX; ++i) {
1462- const auto &eyeSwapChain = m.eyeSwapChains [i];
1463- const int swapChainIndex = m.frameIndex % eyeSwapChain->swapChainLength ;
1464- // Set up OVR layer textures
1465- projection.Textures [i].ColorSwapChain = eyeSwapChain->ovrSwapChain ;
1466- projection.Textures [i].SwapChainIndex = swapChainIndex;
1467- projection.Textures [i].TexCoordsFromTanAngles = ovrMatrix4f_TanAngleMatrixFromProjection (&projectionMatrix);
1479+
1480+ if (!enableExternalSurfaceRender) {
1481+ for (int i = 0 ; i < VRAPI_FRAME_LAYER_EYE_MAX; ++i) {
1482+ const auto &eyeSwapChain = m.eyeSwapChains [i];
1483+ const int swapChainIndex = m.frameIndex % eyeSwapChain->swapChainLength ;
1484+ // Set up OVR layer textures
1485+ projection.Textures [i].ColorSwapChain = eyeSwapChain->ovrSwapChain ;
1486+ projection.Textures [i].SwapChainIndex = swapChainIndex;
1487+ projection.Textures [i].TexCoordsFromTanAngles = ovrMatrix4f_TanAngleMatrixFromProjection (&projectionMatrix);
1488+ }
1489+ } else {
1490+ const int swapChainIndex = externalSurfaceId;
1491+ ovrMatrix4f proj (projectionMatrix);
1492+ // Flip texCoord in vertical when using WebGL frame textures.
1493+ proj.M [1 ][1 ] *= -1 ;
1494+ proj = ovrMatrix4f_TanAngleMatrixFromProjection (&proj);
1495+
1496+ for (int i = 0 ; i < VRAPI_FRAME_LAYER_EYE_MAX; ++i) {
1497+ const auto eyeSwapChain = m.eyeSurfaceSwapChain [swapChainIndex];
1498+ // Set up OVR layer textures
1499+ projection.Textures [i].ColorSwapChain = eyeSwapChain;
1500+ projection.Textures [i].SwapChainIndex = 0 ;
1501+ projection.Textures [i].TexCoordsFromTanAngles = proj;
1502+ }
1503+
1504+ // Switch to the next surface.
1505+ externalSurfaceId = (++externalSurfaceId) % State::EXTERNAL_SURFACE_BUFFER_SIZE;
14681506 }
14691507 layers[layerCount++] = &projection.Header ;
14701508
@@ -1494,6 +1532,11 @@ DeviceDelegateOculusVR::EndFrame(const bool aDiscard) {
14941532 vrapi_SubmitFrame2 (m.ovr , &frameDesc);
14951533}
14961534
1535+ void
1536+ DeviceDelegateOculusVR::EnableExternalSurfaceRender (bool aEnable) {
1537+ enableExternalSurfaceRender = aEnable;
1538+ }
1539+
14971540VRLayerQuadPtr
14981541DeviceDelegateOculusVR::CreateLayerQuad (int32_t aWidth, int32_t aHeight,
14991542 VRLayerSurface::SurfaceType aSurfaceType) {
@@ -1671,6 +1714,13 @@ DeviceDelegateOculusVR::LeaveVR() {
16711714 for (int i = 0 ; i < VRAPI_EYE_COUNT; ++i) {
16721715 m.eyeSwapChains [i]->Destroy ();
16731716 }
1717+ for (int i = 0 ; i < DeviceDelegateOculusVR::State::EXTERNAL_SURFACE_BUFFER_SIZE; ++i) {
1718+ if (m.eyeSurfaceSwapChain [i]) {
1719+ vrapi_DestroyTextureSwapChain (m.eyeSurfaceSwapChain [i]);
1720+ m.eyeSurfaceSwapChain [i] = nullptr ;
1721+ VRBrowser::ReleaseExternalVRSurfaces ();
1722+ }
1723+ }
16741724 if (m.cubeLayer ) {
16751725 m.cubeLayer ->Destroy ();
16761726 }
@@ -1698,7 +1748,8 @@ DeviceDelegateOculusVR::ExitApp() {
16981748 return true ;
16991749}
17001750
1701- DeviceDelegateOculusVR::DeviceDelegateOculusVR (State &aState) : m(aState) {}
1751+ DeviceDelegateOculusVR::DeviceDelegateOculusVR (State &aState) : m(aState),
1752+ externalSurfaceId(0 ), enableExternalSurfaceRender(false ) {}
17021753
17031754DeviceDelegateOculusVR::~DeviceDelegateOculusVR () { m.Shutdown (); }
17041755
0 commit comments