void StimulusDisplay::addContext(int _context_id){ context_ids.push_back(_context_id); int contextIndex = context_ids.size() - 1; OpenGLContextLock ctxLock = opengl_context_manager->setCurrent(_context_id); getCurrentViewportSize(bufferWidths[contextIndex], bufferHeights[contextIndex]); CVDisplayLinkRef dl; if (kCVReturnSuccess != CVDisplayLinkCreateWithActiveCGDisplays(&dl)) { throw SimpleException("Unable to create display link"); } displayLinks.push_back(dl); displayLinkContexts.emplace_back(new DisplayLinkContext(this, contextIndex)); if (kCVReturnSuccess != CVDisplayLinkSetOutputCallback(dl, &StimulusDisplay::displayLinkCallback, displayLinkContexts.back().get())) { throw SimpleException("Unable to set display link output callback"); } if (kCVReturnSuccess != opengl_context_manager->prepareDisplayLinkForContext(dl, _context_id)) { throw SimpleException("Unable to associate display link with OpenGL context"); } if (0 == contextIndex) { setMainDisplayRefreshRate(); allocateBufferStorage(); } }
void StimulusDisplay::allocateBufferStorage(int contextIndex) { if (!(glewIsSupported("GL_EXT_framebuffer_object") && glewIsSupported("GL_EXT_framebuffer_blit"))) { throw SimpleException("renderer does not support required OpenGL framebuffer extensions"); } glGenFramebuffersEXT(1, &framebuffers[contextIndex]); glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, framebuffers[contextIndex]); glGenRenderbuffersEXT(1, &renderbuffers[contextIndex]); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, renderbuffers[contextIndex]); getCurrentViewportSize(bufferWidths[contextIndex], bufferHeights[contextIndex]); glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, bufferWidths[contextIndex], bufferHeights[contextIndex]); glFramebufferRenderbufferEXT(GL_DRAW_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_RENDERBUFFER_EXT, renderbuffers[contextIndex]); GLenum status = glCheckFramebufferStatusEXT(GL_DRAW_FRAMEBUFFER_EXT); if (GL_FRAMEBUFFER_COMPLETE_EXT != status) { throw SimpleException("OpenGL framebuffer setup failed"); } glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0); }