History

Fixing overlay rendering in plugins

Brad Davis committed 11 years ago

Commit 7ca63a41 parent 267d3391 file · +83 −46Browse files

Showing the changes to interface/src/Application.cpp (1 of 1 file). Show all

Unified · Split

interface/src/Application.cpp+83−46
@@ -173,6 +173,22 @@ public: · 172 unmodified lines
173173 };
174174
175175
176+template <typename F>
177+void with_stable_stack_check(F f) {
178+ GLint mvDepth, prDepth;
179+ glGetIntegerv(GL_MODELVIEW_STACK_DEPTH, &mvDepth);
180+ glGetIntegerv(GL_PROJECTION_STACK_DEPTH, &prDepth);
181+ f();
182+ GLint mvDepthFinal, prDepthFinal;
183+ glGetIntegerv(GL_MODELVIEW_STACK_DEPTH, &mvDepthFinal);
184+ glGetIntegerv(GL_PROJECTION_STACK_DEPTH, &prDepthFinal);
185+ Q_ASSERT(mvDepth == mvDepthFinal);
186+ Q_ASSERT(prDepth == prDepthFinal);
187+ if (mvDepth != mvDepthFinal || prDepth != prDepthFinal) {
188+ qFatal("Failed to preserve GL stack depth");
189+ }
190+}
191+
176192 using namespace std;
177193
178194 // Starfield information
@@ -839,14 +855,12 @@ void Application::initializeUi() { · 660 unmodified lines
839855
840856 auto offscreenUi = DependencyManager::get<OffscreenUi>();
841857 offscreenUi->create(_offscreenContext->getContext());
842− offscreenUi->resize(fromGlm(getActiveDisplayPlugin()->getCanvasSize()));
843858 offscreenUi->setProxyWindow(_window->windowHandle());
844859 offscreenUi->setBaseUrl(QUrl::fromLocalFile(PathUtils::resourcesPath() + "/qml/"));
845860 offscreenUi->load("Root.qml");
846861 offscreenUi->load("RootMenu.qml");
847862 VrMenu::load();
848863 VrMenu::executeQueuedLambdas();
849− offscreenUi->setMouseTranslator(getActiveDisplayPlugin()->getMouseTranslator());
850864 offscreenUi->resume();
851865 connect(_window, &MainWindow::windowGeometryChanged, [this](const QRect & r){
852866 static qreal oldDevicePixelRatio = 0;
@@ -960,10 +974,11 @@ void Application::paintGL() { · 107 unmodified lines
960974 auto finalFbo = primaryFbo;
961975 glBindFramebuffer(GL_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(primaryFbo));
962976 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
963− glPushMatrix();
977+ // Viewport is assigned to the size of the framebuffer
978+ QSize size = DependencyManager::get<TextureCache>()->getFrameBufferSize();
979+
980+ // Primary scene rendering
964981 {
965− // Viewport is assigned to the size of the framebuffer
966− QSize size = DependencyManager::get<TextureCache>()->getFrameBufferSize();
967982 if (displayPlugin->isStereo()) {
968983 QRect r(QPoint(0, 0), QSize(size.width() / 2, size.height()));
969984 glEnable(GL_SCISSOR_TEST);
@@ -978,14 +993,11 @@ void Application::paintGL() { · 8 unmodified lines
978993 Camera eyeCamera;
979994 eyeCamera.setTransform(displayPlugin->getModelview(eye, _myCamera.getTransform()));
980995 eyeCamera.setProjection(displayPlugin->getProjection(eye, _myCamera.getProjection()));
981−
982996 displaySide(&renderArgs, eyeCamera);
983−
984997 if (Menu::getInstance()->isOptionChecked(MenuOption::Mirror) &&
985998 !Menu::getInstance()->isOptionChecked(MenuOption::FullscreenMirror)) {
986999 renderRearViewMirror(&renderArgs, _mirrorViewRect);
9871000 }
988−
9891001 }, [&] {
9901002 r.moveLeft(r.width());
9911003 });
@@ -998,8 +1010,11 @@ void Application::paintGL() { · 6 unmodified lines
9981010 renderRearViewMirror(&renderArgs, _mirrorViewRect);
9991011 }
10001012 }
1001− finalFbo = DependencyManager::get<GlowEffect>()->render(&renderArgs);
1013+ }
10021014
1015+ // Overlay Composition
1016+ finalFbo = DependencyManager::get<GlowEffect>()->render(&renderArgs);
1017+ {
10031018 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, gpu::GLBackend::getFramebufferID(finalFbo));
10041019 if (displayPlugin->isStereo()) {
10051020 QSize size = DependencyManager::get<TextureCache>()->getFrameBufferSize();
@@ -1008,7 +1023,7 @@ void Application::paintGL() { · 2 unmodified lines
10081023 for_each_eye([&](Eye eye) {
10091024 glViewport(r.x(), r.y(), r.width(), r.height());
10101025 if (displayPlugin->isHmd()) {
1011− _compositor.displayOverlayTexture(&renderArgs);
1026+ _compositor.displayOverlayTextureHmd(&renderArgs, eye);
10121027 } else {
10131028 _compositor.displayOverlayTexture(&renderArgs);
10141029 }
@@ -1020,7 +1035,6 @@ void Application::paintGL() { · 5 unmodified lines
10201035 _compositor.displayOverlayTexture(&renderArgs);
10211036 }
10221037 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
1023− glFinish();
10241038 }
10251039
10261040 #if 0
@@ -1031,25 +1045,26 @@ void Application::paintGL() { · 4 unmodified lines
10311045 renderArgs._renderMode = RenderArgs::NORMAL_RENDER_MODE;
10321046 #endif
10331047
1034− // Ensure the rendering context commands are completed when rendering
1035− GLsync sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
1036− _offscreenContext->doneCurrent();
1037− Q_ASSERT(!QOpenGLContext::currentContext());
1038−
1039− GLuint finalTexture = gpu::GLBackend::getTextureID(finalFbo->getRenderBuffer(0));
1040− displayPlugin->preDisplay();
1041−
1042− Q_ASSERT(QOpenGLContext::currentContext());
1043− glWaitSync(sync, 0, GL_TIMEOUT_IGNORED);
1044− glDeleteSync(sync);
1045−
1046− displayPlugin->display(finalTexture, finalFbo->getSize());
1047−
1048− displayPlugin->finishFrame();
1049− Q_ASSERT(!QOpenGLContext::currentContext());
1050− _offscreenContext->makeCurrent();
1051− _frameCount++;
1052− Stats::getInstance()->setRenderDetails(renderArgs._details);
1048+ // deliver final composited scene to the display plugin
1049+ {
1050+ GLuint finalTexture = gpu::GLBackend::getTextureID(finalFbo->getRenderBuffer(0));
1051+ uvec2 finalSize = toGlm(size);
1052+ // Ensure the rendering context commands are completed when rendering
1053+ GLsync sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
1054+ _offscreenContext->doneCurrent();
1055+ Q_ASSERT(!QOpenGLContext::currentContext());
1056+ displayPlugin->preDisplay();
1057+ Q_ASSERT(QOpenGLContext::currentContext());
1058+ // FIXME? make the sync a parameter to preDisplay and let the plugin manage this
1059+ glWaitSync(sync, 0, GL_TIMEOUT_IGNORED);
1060+ glDeleteSync(sync);
1061+ displayPlugin->display(finalTexture, finalSize);
1062+ displayPlugin->finishFrame();
1063+ Q_ASSERT(!QOpenGLContext::currentContext());
1064+ _offscreenContext->makeCurrent();
1065+ _frameCount++;
1066+ Stats::getInstance()->setRenderDetails(renderArgs._details);
1067+ }
10531068 }
10541069
10551070 void Application::runTests() {
@@ -3338,16 +3353,18 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se · 2282 unmodified lines
33383353
33393354 // Load the legacy GL stacks, used by entities (for now)
33403355 glMatrixMode(GL_PROJECTION);
3356+ glPushMatrix();
33413357 glLoadMatrixf(glm::value_ptr(theCamera.getProjection()));
33423358
33433359 glMatrixMode(GL_MODELVIEW);
3360+ glPushMatrix();
33443361 glLoadMatrixf(glm::value_ptr(glm::inverse(theCamera.getTransform())));
33453362
33463363 // FIXME just flip the texture coordinates
33473364 // flip x if in mirror mode (also requires reversing winding order for backface culling)
33483365 if (theCamera.getMode() == CAMERA_MODE_MIRROR) {
3349− //glScalef(-1.0f, 1.0f, 1.0f);
3350− //glFrontFace(GL_CW);
3366+ glScalef(-1.0f, 1.0f, 1.0f);
3367+ glFrontFace(GL_CW);
33513368 } else {
33523369 glFrontFace(GL_CCW);
33533370 }
@@ -3360,7 +3377,7 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se · 6 unmodified lines
33603377 viewTransform.setTranslation(theCamera.getPosition());
33613378 viewTransform.setRotation(rotation);
33623379 if (theCamera.getMode() == CAMERA_MODE_MIRROR) {
3363−// viewTransform.setScale(Transform::Vec3(-1.0f, 1.0f, 1.0f));
3380+ viewTransform.setScale(Transform::Vec3(-1.0f, 1.0f, 1.0f));
33643381 }
33653382
33663383 setViewTransform(viewTransform);
@@ -3524,6 +3541,7 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se · 157 unmodified lines
35243541 PerformanceTimer perfTimer("octreeFades");
35253542 PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),
35263543 "Application::displaySide() ... octree fades...");
3544+
35273545 _octreeFadesLock.lockForWrite();
35283546 for(std::vector<OctreeFade>::iterator fade = _octreeFades.begin(); fade != _octreeFades.end();) {
35293547 fade->render(renderArgs);
@@ -3547,6 +3565,12 @@ void Application::displaySide(RenderArgs* renderArgs, Camera& theCamera, bool se · 17 unmodified lines
35473565 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
35483566 }
35493567
3568+ glMatrixMode(GL_PROJECTION);
3569+ glPopMatrix();
3570+ glLoadMatrixf(glm::value_ptr(theCamera.getProjection()));
3571+
3572+ glMatrixMode(GL_MODELVIEW);
3573+ glPopMatrix();
35503574 activeRenderingThread = nullptr;
35513575 }
35523576
@@ -4755,7 +4779,10 @@ const DisplayPlugin * Application::getActiveDisplayPlugin() const { · 1202 unmodified lines
47554779
47564780 void Application::updateDisplayMode() {
47574781 auto menu = Menu::getInstance();
4758− DisplayPluginPointer newDisplayPlugin;
4782+ auto displayPlugins = getDisplayPlugins();
4783+
4784+ // Default to the first item on the list, in case none of the menu items match
4785+ DisplayPluginPointer newDisplayPlugin = displayPlugins.at(0);
47594786 foreach(DisplayPluginPointer displayPlugin, getDisplayPlugins()) {
47604787 QString name = displayPlugin->getName();
47614788 QAction* action = menu->getActionForOption(name);
@@ -4764,30 +4791,40 @@ void Application::updateDisplayMode() { · 2 unmodified lines
47644791 break;
47654792 }
47664793 }
4767− if (_displayPlugin != newDisplayPlugin) {
4768− DependencyManager::get<OffscreenUi>()->setProxyWindow(nullptr);
4794+
4795+ auto offscreenUi = DependencyManager::get<OffscreenUi>();
4796+ DisplayPluginPointer oldDisplayPlugin = _displayPlugin;
4797+ if (oldDisplayPlugin != newDisplayPlugin) {
4798+ if (oldDisplayPlugin) {
4799+ oldDisplayPlugin->removeEventFilter(offscreenUi.data());
4800+ oldDisplayPlugin->removeEventFilter(qApp);
4801+ }
47694802
47704803 if (newDisplayPlugin) {
47714804 _offscreenContext->makeCurrent();
47724805 newDisplayPlugin->activate(this);
4773− newDisplayPlugin->installEventFilter(DependencyManager::get<OffscreenUi>().data());
4806+
4807+ _offscreenContext->makeCurrent();
4808+ newDisplayPlugin->installEventFilter(offscreenUi.data());
47744809 newDisplayPlugin->installEventFilter(qApp);
47754810 QWindow* pluginWindow = newDisplayPlugin->getWindow();
47764811 if (pluginWindow) {
47774812 DependencyManager::get<OffscreenUi>()->setProxyWindow(pluginWindow);
4778− }
4813+ } else {
4814+ DependencyManager::get<OffscreenUi>()->setProxyWindow(nullptr);
4815+ }
4816+ offscreenUi->setMouseTranslator(newDisplayPlugin->getMouseTranslator());
4817+ offscreenUi->resize(fromGlm(newDisplayPlugin->getCanvasSize()));
47794818 _offscreenContext->makeCurrent();
4780− }
4819+ }
47814820
4782− std::swap(newDisplayPlugin, _displayPlugin);
4783− if (newDisplayPlugin) {
4784− newDisplayPlugin->removeEventFilter(DependencyManager::get<OffscreenUi>().data());
4785− newDisplayPlugin->deactivate();
4821+ oldDisplayPlugin = _displayPlugin;
4822+ _displayPlugin = newDisplayPlugin;
4823+
4824+ if (oldDisplayPlugin) {
4825+ oldDisplayPlugin->deactivate();
47864826 _offscreenContext->makeCurrent();
47874827 }
4788−
4789− auto offscreenUi = DependencyManager::get<OffscreenUi>();
4790− offscreenUi->setMouseTranslator(getActiveDisplayPlugin()->getMouseTranslator());
47914828 }
47924829 Q_ASSERT_X(_displayPlugin, "Application::updateDisplayMode", "could not find an activated display plugin");
47934830 }