fix quest vr threading

This commit is contained in:
2019-08-01 18:22:31 +02:00
parent 945028f171
commit 32667b709e
5 changed files with 51 additions and 43 deletions

View File

@@ -97,31 +97,36 @@ void oculus_init(JavaVM* vm, JNIEnv* jni, jobject activity_class)
void oculus_init_vr(EGLDisplay display, EGLContext context, ANativeWindow* surface)
{
LOG("init swapchain");
int rtt_w = vrapi_GetSystemPropertyInt(&java, VRAPI_SYS_PROP_SUGGESTED_EYE_TEXTURE_WIDTH);
int rtt_h = vrapi_GetSystemPropertyInt(&java, VRAPI_SYS_PROP_SUGGESTED_EYE_TEXTURE_HEIGHT);
LOG("ovr suggested texture size %d %d", rtt_w, rtt_h);
for (int eye = 0; eye < 2; eye++)
{
swap_chain[eye] = vrapi_CreateTextureSwapChain3(VRAPI_TEXTURE_TYPE_2D, GL_RGBA8, rtt_w, rtt_h, 1, 3);
swap_chain_count = vrapi_GetTextureSwapChainLength(swap_chain[eye]);
for (int i = 0; i < swap_chain_count; i++)
App::I->render_task([&]{
LOG("init swapchain");
int rtt_w = vrapi_GetSystemPropertyInt(&java, VRAPI_SYS_PROP_SUGGESTED_EYE_TEXTURE_WIDTH);
int rtt_h = vrapi_GetSystemPropertyInt(&java, VRAPI_SYS_PROP_SUGGESTED_EYE_TEXTURE_HEIGHT);
LOG("ovr suggested texture size %d %d", rtt_w, rtt_h);
for (int eye = 0; eye < 2; eye++)
{
auto texid = vrapi_GetTextureSwapChainHandle(swap_chain[eye], i);
if (ovr_eyes[i][eye].create(rtt_w, rtt_h, texid, GL_RGBA8, true))
swap_chain[eye] = vrapi_CreateTextureSwapChain3(VRAPI_TEXTURE_TYPE_2D, GL_RGBA8, rtt_w, rtt_h, 1, 3);
swap_chain_count = vrapi_GetTextureSwapChainLength(swap_chain[eye]);
for (int i = 0; i < swap_chain_count; i++)
{
ovr_eyes[i][eye].bindTexture();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
ovr_eyes[i][eye].unbindTexture();
LOG("create eye %d", eye);
}
else
{
LOG("FAILED create fb for eye %d", eye);
auto texid = vrapi_GetTextureSwapChainHandle(swap_chain[eye], i);
if (ovr_eyes[i][eye].create(rtt_w, rtt_h, texid, GL_RGBA8, true))
{
App::I->render_task([&]
{
ovr_eyes[i][eye].bindTexture();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
ovr_eyes[i][eye].unbindTexture();
LOG("create eye %d", eye);
});
}
else
{
LOG("FAILED create fb for eye %d", eye);
}
}
}
}
});
LOG("vrapi_DefaultModeParms");
ovrModeParms parms = vrapi_DefaultModeParms( &java );
@@ -207,7 +212,7 @@ void oculus_draw(float dt)
controllers[0].update_state(predictedDisplayTime, head_pos);
App::I->vr_controllers[0] = controllers[0];
}
App::I->vr_head = pose;
App::I->vr_head = glm::translate(head_pos) * pose;
App::I->vr_update(dt);
// update hmd
@@ -225,7 +230,7 @@ void oculus_draw(float dt)
glm::mat4 proj = glm::make_mat4(reinterpret_cast<const float*>(&proj_ovr));
auto view_ovr = ovrMatrix4f_Transpose(&tracking.Eye[eye].ViewMatrix);
glm::mat4 view = glm::make_mat4(reinterpret_cast<const float*>(&view_ovr));
App::I->vr_draw(proj, view, pose);
App::I->vr_draw(proj, view, glm::translate(head_pos) * pose);
rtt.unbindFramebuffer();
layer.Textures[eye].ColorSwapChain = swap_chain[eye];
layer.Textures[eye].SwapChainIndex = swap_chain_index;

View File

@@ -383,11 +383,21 @@ static void engine_vr_loop()
LOG("hmd renderer terminated");
}
static void engine_start_vr()
static void engine_start_vr_thread()
{
std::thread(engine_vr_loop).detach();
}
static void engine_start_vr_mode()
{
#ifdef __QUEST__
LOG("QUEST init VR");
oculus_init_vr(g_engine.display, g_engine.context, g_engine.app->window);
#elif __FOCUS__
wave_init_vr(g_engine.display, g_engine.context, g_engine.app->window);
#endif
}
/**
* Initialize an EGL context for the current display.
*/
@@ -569,14 +579,6 @@ static int engine_init_display(struct engine* engine) {
g_display = display;
g_context = context;
#ifdef __QUEST__
App::I->render_thread_id = std::this_thread::get_id();
LOG("QUEST init VR");
oculus_init_vr(display, context, engine->app->window);
#elif __FOCUS__
wave_init_vr(display, context, engine->app->window);
#endif
if (resuming_context)
{
LOG("RESUME APP");
@@ -584,6 +586,7 @@ static int engine_init_display(struct engine* engine) {
LOG("release egl context");
eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
mutex.unlock();
engine_start_vr_mode();
return 0;
}
@@ -702,12 +705,13 @@ static int engine_init_display(struct engine* engine) {
LOG("start render threads");
App::I->render_thread_start();
LOG("start ui thread");
App::I->ui_thread_start();
App::I->ui_sync();
LOG("start vr thread");
engine_start_vr();
engine_start_vr_mode();
engine_start_vr_thread();
#else
float density = get_display_density();
LOG("density %f", density);