void QSGMaterialShader::compile() { Q_ASSERT_X(!m_program.isLinked(), "QSGSMaterialShader::compile()", "Compile called multiple times!"); program()->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShader()); program()->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShader()); char const *const *attr = attributeNames(); #ifndef QT_NO_DEBUG int maxVertexAttribs = 0; QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); funcs->glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &maxVertexAttribs); for (int i = 0; attr[i]; ++i) { if (i >= maxVertexAttribs) { qFatal("List of attribute names is either too long or not null-terminated.\n" "Maximum number of attributes on this hardware is %i.\n" "Vertex shader:\n%s\n" "Fragment shader:\n%s\n", maxVertexAttribs, vertexShader(), fragmentShader()); } if (*attr[i]) program()->bindAttributeLocation(attr[i], i); } #else for (int i = 0; attr[i]; ++i) { if (*attr[i]) program()->bindAttributeLocation(attr[i], i); } #endif if (!program()->link()) { qWarning("QSGMaterialShader: Shader compilation failed:"); qWarning() << program()->log(); } }
QOpenGLExtensionMatcher::QOpenGLExtensionMatcher() { QOpenGLContext *ctx = QOpenGLContext::currentContext(); QOpenGLFunctions *funcs = ctx->functions(); const char *extensionStr = 0; if (ctx->isOpenGLES() || ctx->format().majorVersion() < 3) extensionStr = reinterpret_cast<const char *>(funcs->glGetString(GL_EXTENSIONS)); if (extensionStr) { QByteArray ba(extensionStr); QList<QByteArray> extensions = ba.split(' '); m_extensions = extensions.toSet(); } else { #ifdef QT_OPENGL_3 // clear error state while (funcs->glGetError()) {} if (ctx) { qt_glGetStringi glGetStringi = (qt_glGetStringi)ctx->getProcAddress("glGetStringi"); if (!glGetStringi) return; GLint numExtensions; funcs->glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions); for (int i = 0; i < numExtensions; ++i) { const char *str = reinterpret_cast<const char *>(glGetStringi(GL_EXTENSIONS, i)); m_extensions.insert(str); } } #endif // QT_OPENGL_3 } }
void ShaderBoldLineGen::set_width(float32 w) { QOpenGLFunctions* ogl = QOpenGLContext::currentContext()->functions(); GLint viewport[4]; ogl->glGetIntegerv(GL_VIEWPORT, viewport); QSizeF wd(w / float32(viewport[2]), w / float32(viewport[3])); prg_.setUniformValue(unif_width_, wd); }
void TFInteg2DGL::integrate(const float *colormap, int resolution, float basesize, float stepsize) { if (!texFull || resolution != texFull->width()) newResources(resolution); QOpenGLFunctions* f = QOpenGLContext::currentContext()->functions(); tex1d->setData(QOpenGLTexture::RGBA, QOpenGLTexture::Float32, colormap); // framebuffer object GLint oFbo, viewport[4], activeTex, oTex; newFbo(); f->glGetIntegerv(GL_FRAMEBUFFER_BINDING, &oFbo); f->glBindFramebuffer(GL_FRAMEBUFFER, *fbo); f->glClear(GL_COLOR_BUFFER_BIT); // viewport f->glGetIntegerv(GL_VIEWPORT, viewport); f->glViewport(0, 0, resolution, resolution); // 1d texture f->glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTex); f->glActiveTexture(GL_TEXTURE0); f->glGetIntegerv(GL_TEXTURE_1D, &oTex); f->glBindTexture(GL_TEXTURE_1D, tex1d->textureId()); // paint painter.recreateVAO(); painter.paint("tf1d", 0, "resolution", resolution, "basesize", basesize, "segLen", stepsize); // clean f->glActiveTexture(GL_TEXTURE0); f->glBindTexture(GL_TEXTURE_1D, oTex); f->glActiveTexture(activeTex); // std::unique_ptr<GLubyte[]> pixels(new GLubyte [resolution * resolution * 4]); // f->glReadPixels(0, 0, resolution, resolution, GL_RGBA, GL_UNSIGNED_BYTE, pixels.get()); // QImage image(pixels.get(), resolution, resolution, QImage::Format_RGBA8888); // static QLabel label; // label.resize(resolution, resolution); // label.setPixmap(QPixmap::fromImage(image.mirrored())); // label.show(); f->glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); f->glBindFramebuffer(GL_FRAMEBUFFER, oFbo); }
void bind() { QOpenGLFunctions *functions = QOpenGLContext::currentContext()->functions(); QMutexLocker lock(&m_frameMutex); if (m_frame.isValid()) { if (m_frame.map(QAbstractVideoBuffer::ReadOnly)) { QSize textureSize = m_frame.size(); int stride = m_frame.bytesPerLine(); switch (m_frame.pixelFormat()) { case QVideoFrame::Format_RGB565: stride /= 2; break; default: stride /= 4; } m_width = qreal(m_frame.width()) / stride; textureSize.setWidth(stride); if (m_textureSize != textureSize) { if (!m_textureSize.isEmpty()) functions->glDeleteTextures(1, &m_textureId); functions->glGenTextures(1, &m_textureId); m_textureSize = textureSize; } GLint dataType = GL_UNSIGNED_BYTE; GLint dataFormat = GL_RGBA; if (m_frame.pixelFormat() == QVideoFrame::Format_RGB565) { dataType = GL_UNSIGNED_SHORT_5_6_5; dataFormat = GL_RGB; } GLint previousAlignment; functions->glGetIntegerv(GL_UNPACK_ALIGNMENT, &previousAlignment); functions->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); functions->glActiveTexture(GL_TEXTURE0); functions->glBindTexture(GL_TEXTURE_2D, m_textureId); functions->glTexImage2D(GL_TEXTURE_2D, 0, dataFormat, m_textureSize.width(), m_textureSize.height(), 0, dataFormat, dataType, m_frame.bits()); functions->glPixelStorei(GL_UNPACK_ALIGNMENT, previousAlignment); functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); m_frame.unmap(); } m_frame = QVideoFrame(); } else { functions->glActiveTexture(GL_TEXTURE0); functions->glBindTexture(GL_TEXTURE_2D, m_textureId); } }
void QSGVideoMaterial_YUV::bind() { QOpenGLFunctions *functions = QOpenGLContext::currentContext()->functions(); QMutexLocker lock(&m_frameMutex); if (m_frame.isValid()) { if (m_frame.map(QAbstractVideoBuffer::ReadOnly)) { int fw = m_frame.width(); int fh = m_frame.height(); // Frame has changed size, recreate textures... if (m_textureSize != m_frame.size()) { if (!m_textureSize.isEmpty()) functions->glDeleteTextures(m_planeCount, m_textureIds); functions->glGenTextures(m_planeCount, m_textureIds); m_textureSize = m_frame.size(); } GLint previousAlignment; functions->glGetIntegerv(GL_UNPACK_ALIGNMENT, &previousAlignment); functions->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); if (m_format.pixelFormat() == QVideoFrame::Format_NV12 || m_format.pixelFormat() == QVideoFrame::Format_NV21) { const int y = 0; const int uv = 1; m_planeWidth[0] = m_planeWidth[1] = qreal(fw) / m_frame.bytesPerLine(y); functions->glActiveTexture(GL_TEXTURE1); bindTexture(m_textureIds[1], m_frame.bytesPerLine(uv) / 2, fh / 2, m_frame.bits(uv), GL_LUMINANCE_ALPHA); functions->glActiveTexture(GL_TEXTURE0); // Finish with 0 as default texture unit bindTexture(m_textureIds[0], m_frame.bytesPerLine(y), fh, m_frame.bits(y), GL_LUMINANCE); } else { // YUV420P || YV12 const int y = 0; const int u = m_frame.pixelFormat() == QVideoFrame::Format_YUV420P ? 1 : 2; const int v = m_frame.pixelFormat() == QVideoFrame::Format_YUV420P ? 2 : 1; m_planeWidth[0] = qreal(fw) / m_frame.bytesPerLine(y); m_planeWidth[1] = m_planeWidth[2] = qreal(fw) / (2 * m_frame.bytesPerLine(u)); functions->glActiveTexture(GL_TEXTURE1); bindTexture(m_textureIds[1], m_frame.bytesPerLine(u), fh / 2, m_frame.bits(u), GL_LUMINANCE); functions->glActiveTexture(GL_TEXTURE2); bindTexture(m_textureIds[2], m_frame.bytesPerLine(v), fh / 2, m_frame.bits(v), GL_LUMINANCE); functions->glActiveTexture(GL_TEXTURE0); // Finish with 0 as default texture unit bindTexture(m_textureIds[0], m_frame.bytesPerLine(y), fh, m_frame.bits(y), GL_LUMINANCE); } functions->glPixelStorei(GL_UNPACK_ALIGNMENT, previousAlignment); m_frame.unmap(); } m_frame = QVideoFrame(); } else { // Go backwards to finish with GL_TEXTURE0 for (int i = m_planeCount - 1; i >= 0; --i) { functions->glActiveTexture(GL_TEXTURE0 + i); functions->glBindTexture(GL_TEXTURE_2D, m_textureIds[i]); } } }