[Interest] Troubles with QQuickItem and custom QSGTexture bind

Benjamin Balga balga.benjamin at gmail.com
Mon Nov 27 22:52:01 CET 2017


Hello,

I'm looking for some advices on how to use custom textures with QQuickItems, to check I'm doing things wrong or not...

I don't know OpenGL very much, I'm just trying to port a python demo code into a QQuickItem, which uses textures for custom data. Here some of my code:


Textures inherits QSGTexture

void DataTexture::bind()
{
    QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
    if (!m_textureId) {
        f->glGenTextures(1, &m_textureId);
        f->glBindTexture(GL_TEXTURE_2D, m_textureId);

        f->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        f->glPixelStorei(GL_PACK_ALIGNMENT, 1);
        f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
        f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);

        f->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F,
                        m_width, m_height, 0, GL_RGBA, GL_FLOAT, &m_data);
        return;
    }

    f->glBindTexture(GL_TEXTURE_2D, m_textureId);
    f->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F,
                    m_width, m_height, 0, GL_RGBA, GL_FLOAT, &m_data);
}

void Atlas::bind()
{
    QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
    if (!m_textureId) {
        f->glGenTextures(1, &m_textureId);
        f->glBindTexture(GL_TEXTURE_2D, m_textureId);

        f->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        f->glPixelStorei(GL_PACK_ALIGNMENT, 1);
        f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        f->glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

        f->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F,
                        m_width, m_rows, 0, GL_RGBA, GL_FLOAT, &m_data);
        return;
    }

    f->glBindTexture(GL_TEXTURE_2D, m_textureId);
    f->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F,
                    m_width, m_rows, 0, GL_RGBA, GL_FLOAT, &m_data);
}


Here is how I bind textures in my shader :

void MaterialShader::updateState(const QSGMaterialShader::RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
{
    QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
    f->glUniform1i(m_uniformsLoc, 0);
    f->glUniform1i(m_dashAtlasLoc, 1);

    f->glActiveTexture(GL_TEXTURE0);
    m_texture->bind(); // DataTexture

    f->glActiveTexture(GL_TEXTURE1);
    m_atlas->bind(); // Altas texture

    f->glActiveTexture(GL_TEXTURE0);

    ... (program()->setUniformValue() and stuff)
}

And here are parts of my vertex and fragment shaders (just the uniforms, I guess the rest isn't useful here?)

// vertex shader
uniform highp mat4 u_mvmatrix;
uniform highp mat4 u_pmatrix;
uniform highp sampler2D u_uniforms;
uniform highp vec3      u_uniforms_shape;

// fragment shader
//uniform highp sampler2D u_uniforms;
uniform highp sampler2D u_dash_atlas;


Is that code "right"? Are the binds in the right place? I couldn't find examples using custom textures, but from Qt sources and others, it doesn't seems wrong to me.

The problem I have is that the second texture (m_atlas) doesn't seems to work, I don't get the right values from it. If in the python code I don't load that same texture, I get similar "errors" in the graphic output, which would mean that with Qt the second texture isn't "loaded"??

The first texture (u_uniforms) is working, so I don't understand why the second one isn't cooperating with me.

I use Qt5.8.0 under macOS Mavericks (OpenGL 2.1).
The other problem is that the whole thing doesn't work with Qt5.9 / macOS Sierra with the exact same code. Other OpenGL version maybe? I will check that tomorrow. But that would maybe mean I'm not doing things right with the textures?


I don't know what to do to fix that now, I'm just doing trial-and-error, so any hint or advice is welcome!
Thanks!

Best regards,
Benjamin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20171127/09aec2c9/attachment.html>


More information about the Interest mailing list