Unrecognized profile specifier "flat"

Hello, I am trying to pass an int from my vertex shader to my fragment shader but :

  • when I dont use flat the value stay as 0 in my fragment shader but I get no error
  • when I use flat I get this error :unrecognized profile specifier “flat”

Here is my shaders :

static const char *vertexShaderSourceCore =
            "#version 330 core\n"
            "uniform mat4 transform[10];\n"
             "uniform bool isQuad;\n"
             "flat varying out int instanceID;\n"
            "in vec3 aPos;\n"
            "void main()\n"
            "{\n"
            "instanceID = gl_InstanceID+isQuad*5;\n"
            "   gl_Position = transform[instanceID]* vec4(aPos, 1.0);\n"
            "}\0";
static const char *fragmentShaderSourceCore =
            "#version 330 core\n"
            "varying out vec4 FragColor;\n"
            "uniform vec4 color[10];\n"
            "flat in int instanceID;\n"
            "void main()\n"
            "{\n"
            "   FragColor = color[instanceID];\n"
            "}\n\0";

by the way I’m using opengl in qt so maybe I lack a header but it’s unlikly…
Any idea why I get this error ?
Thanks

Full debug output :

QOpenGLShader::compile(Vertex): 0(4) : warning C7022: unrecognized profile specifier "flat"

0(4) : error C0502: syntax error at token "flat"

0(4) : warning C7514: OpenGL does not allow varying of type int

0(8) : warning C7532: global variable gl_InstanceID requires "#version 140" or later

0(8) : warning C0000: ... or #extension GL_EXT_draw_instanced : enable

0(8) : warning C0000: ... or #extension GL_EXT_gpu_shader4 : enable

0(8) : warning C7011: implicit cast from "bool" to "int"

*** Problematic Vertex shader source code ***

#define lowp

#define mediump

#define highp

#line 1

uniform mat4 transform[10];

uniform bool isQuad;

flat varying out int instanceID;

in vec3 aPos;

void main()

{

instanceID = gl_InstanceID+isQuad*5;

gl_Position = transform[instanceID]* vec4(aPos, 1.0);

}

***

QOpenGLShader::compile(Fragment): 0(4) : warning C7022: unrecognized profile specifier "flat"

0(4) : error C0502: syntax error at token "flat"

*** Problematic Fragment shader source code ***

#define lowp

#define mediump

#define highp

#line 1

varying out vec4 FragColor;

uniform vec4 color[10];

flat in int instanceID;

void main()

{

FragColor = color[instanceID];

}

***

QOpenGLShaderProgram::uniformLocation(isQuad): shader program is not linked

QOpenGLShaderProgram::uniformLocation(transform[0]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(color[0]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(transform[1]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(color[1]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(transform[2]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(color[2]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(transform[3]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(color[3]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(transform[4]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(color[4]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(transform[5]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(color[5]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(transform[6]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(color[6]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(transform[7]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(color[7]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(transform[8]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(color[8]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(transform[9]): shader program is not linked

QOpenGLShaderProgram::uniformLocation(color[9]): shader program is not linked

QOpenGLShaderProgram::attributeLocation(aPos): shader program is not linked

QOpenGLShaderProgram::attributeLocation(aPos): shader program is not linked

This line is telling. You asked for #version 330, but the compiler doesn’t seem to be giving it to you. So odds are good something is going wrong either in your compilation code (ie: the string you provide is wrong) or your implementation doesn’t support 3.30 core profile or something else.

Also, varying out is not a thing in GLSL. It was part of an old, outdated extensions, and even then, it was not used in either the vertex or fragment shader languages. So the declaration flat varying out int instanceID was never going to work in 3.30 core.

1 Like

thanks but I still don’t know how to solve my issue, I still get the same error when trying with 140
How can I pass the instance ID to my fragment shader in recent opengl ?

this is my full code, I don’t care if it’s using core or not and I am running this on qt opengl widget

sklgl2d::sklgl2d(QWidget *parent)
    : QOpenGLWidget(parent)
{
    core = QSurfaceFormat::defaultFormat().profile() == QSurfaceFormat::CoreProfile;
}

sklgl2d::~sklgl2d()
{
    makeCurrent();
    delete geo_tri;
    delete geo_quad;
    doneCurrent();
}

static const char *vertexShaderSourceCore =
            "#version 140 core\n"
            "uniform mat4 transform[10];\n"
             "uniform bool isQuad;\n"
             "flat varying out int instanceID;\n"
            "in vec3 aPos;\n"
            "void main()\n"
            "{\n"
            "instanceID = gl_InstanceID+isQuad*5;\n"
            "   gl_Position = transform[instanceID]* vec4(aPos, 1.0);\n"
            "}\0";

static const char *vertexShaderSource =
        "uniform mat4 transform[10];\n"
         "uniform bool isQuad;\n"
         "flat varying out int instanceID;\n"
        "in vec3 aPos;\n"
        "void main()\n"
        "{\n"
        "instanceID = gl_InstanceID+isQuad*5;\n"
        "   gl_Position = transform[instanceID]* vec4(aPos, 1.0);\n"
        "}\0";

static const char *fragmentShaderSourceCore =
            "#version 140 core\n"
            "varying out vec4 FragColor;\n"
            "uniform vec4 color[10];\n"
            "flat in int instanceID;\n"
            "void main()\n"
            "{\n"
            "   FragColor = color[instanceID];\n"
            "}\n\0";

static const char *fragmentShaderSource =
        "varying out vec4 FragColor;\n"
        "uniform vec4 color[10];\n"
        "flat in int instanceID;\n"
        "void main()\n"
        "{\n"
        "   FragColor = color[instanceID];\n"
        "}\n\0";

void sklgl2d::initializeGL()
{
    initializeOpenGLFunctions();
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    if (!program.addShaderFromSourceCode(QOpenGLShader::Vertex, core ? vertexShaderSourceCore : vertexShaderSource))
        close();
    if (!program.addShaderFromSourceCode(QOpenGLShader::Fragment, core ? fragmentShaderSourceCore : fragmentShaderSource))
        close();
    if (!program.link())
        close();
    if (!program.bind())
        close();
    geo_tri = new sklgl2dgeo_tri;
    geo_quad = new sklgl2dgeo_quad;
    for(unsigned int i = 0; i < 10; i++)
    {
        world[i].setToIdentity();
         color[i] = {1.0f,1.0f,1.0f,1.0f};
    }
    //timer.start(12, this);
}


void sklgl2d::paintGL()
{
        glClearColor(0, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        //QTime t; t.start(); float sec = t.msec()/100; R =  abs(sin(sec)); G =  abs(cos(sec)); B =  abs(sin(sec-100));
        const QString isquadname ="isQuad";
        GLint isQuadLocation = program.uniformLocation(isquadname);
         for(unsigned int i = 0; i < 10; i++)
         {
             world[i].setToIdentity();
             world[i].translate(posx[i],posy[i],0.0f);
             const QString transname ="transform[" + QString::number(i) + "]";
            GLint transLocation = program.uniformLocation(transname);
            program.setUniformValue(transLocation, world[i]);

            const QString colname ="color[" + QString::number(i) + "]";
            GLint ColorLocation = program.uniformLocation(colname);
            program.setUniformValue(ColorLocation, color[i]);
         }
        program.setUniformValue(isQuadLocation, 0);
        geo_tri->drawgeo(&program);
        program.setUniformValue(isQuadLocation, 1);
        geo_quad->drawgeo(&program);
}

This is my draw class :

#include "sklgl2dgeo_tri.h"

sklgl2dgeo_tri::sklgl2dgeo_tri()
    : tri_indexBuf(QOpenGLBuffer::IndexBuffer)
{
    initializeOpenGLFunctions();
    // Generate VBOs
    tri_arrayBuf.create();
    tri_indexBuf.create();
    initgeo();
}

sklgl2dgeo_tri::~sklgl2dgeo_tri()
{
    tri_arrayBuf.destroy();
    tri_indexBuf.destroy();
}

void sklgl2dgeo_tri::initgeo()
{

    float tri_vertices[] = {
         0.25f, -0.25f, 0.0f,  // bottom right
        -0.25f, -0.25f, 0.0f,  // bottom left
         0.0f,  0.25f, 0.0f   // top
    };
    GLushort tri_indices[] = {
         0,  1,  2
    };
    // Transfer data to tri VBOs
    tri_arrayBuf.bind();
    tri_arrayBuf.allocate(tri_vertices, 9 * sizeof(float));
    tri_indexBuf.bind();
    tri_indexBuf.allocate(tri_indices, 3 * sizeof(GLushort));
}

void sklgl2dgeo_tri::drawgeo(QOpenGLShaderProgram *program)
{
    QOpenGLExtraFunctions *f = QOpenGLContext::currentContext()->extraFunctions();
    // Tell OpenGL which VBOs to use
    tri_arrayBuf.bind();
    tri_indexBuf.bind();
    // Tell OpenGL programmable pipeline how to locate vertex position data
    int vertexLocation = program->attributeLocation("aPos");
    program->enableAttributeArray(vertexLocation);
    program->setAttributeBuffer(vertexLocation, GL_FLOAT, 0, 3, 3*sizeof(float));
    // Draw
   f->QOpenGLExtraFunctions::glDrawElementsInstanced(GL_TRIANGLE_STRIP, 3, GL_UNSIGNED_SHORT, nullptr,5);
    //glDrawElements(GL_TRIANGLE_STRIP, 3, GL_UNSIGNED_SHORT, nullptr);
}

… you made your code worse. GLSL 1.40 doesn’t have a “core” or “compatibility” version; those didn’t come about until 1.50. So #version 140 core is never valid.

And you didn’t remove the varying out nonsense that is, again, not part of any non-extended version of GLSL. It only works on an old, outdated extension that you are not using.

Stop using non-existent syntax, and maybe your code will work.

And why is there a NUL-terminator inside of your string literal? This feels like some kind of Frankenstein’s code, that you cobbled together fragments of stuff you’ve seen without understanding what all of these parts are doing.

Thanks,
I did cobbled together fragments, but I do understand 90% of what I am doing,
I removed the NUL-terminator inside of my string literal it’s indeed useless

when I don’t use varying I get :
error C5060: out can’t be used with non-varying instanceID
error C5060: out can’t be used with non-varying FragColor
error C0502: syntax error at token “flat”
using varying was the only solution I found to compile the ‘out’ lines in shaders

If I’m not mistaken, you need at least OpenGL 3.2 to use the flat token. The corresponding GLSL version should be 1.5 (therefore you should use version 150 at the top of your shader).

What’s the highest version of OpenGL your graphics card / driver support ?

If you card support at least OpenGL 3.3, my advice is to always ask for a 3.3 OpenGL context and use version 330 at the top of your shader, unless you need more recent versions.

1 Like

I can run :

OpenGL 4.5
OpenGL-ES 3.2

I forced the use on GLSL source core script where I specifyed the version, then I had an Issue with the implicit cast and had to do a proper bool to int cast, removed varying and now it’s working perfectly in version 330.
Thank you a ton silence and reinheart !

This is the modification I had to do :

if (!program.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSourceCore))
//if (!program.addShaderFromSourceCode(QOpenGLShader::Vertex, core ? vertexShaderSourceCore : vertexShaderSource))
    close();
if (!program.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSourceCore))
    close();

and

static const char *vertexShaderSourceCore =
            "#version 330 core\n"
            "uniform mat4 transform[10];\n"
             "uniform bool isQuad;\n"
             "flat  out int instanceID;\n"
            "in vec3 aPos;\n"
            "void main()\n"
            "{\n"
            "instanceID = gl_InstanceID+int(isQuad)*5;\n"
            "   gl_Position = transform[instanceID]* vec4(aPos, 1.0);\n"
            "}\n";

static const char *fragmentShaderSourceCore =
            "#version 330 core\n"
            " out vec4 FragColor;\n"
            "uniform vec4 color[10];\n"
            "flat in int instanceID;\n"
            "void main()\n"
            "{\n"
            "   FragColor = color[instanceID];\n"
            "}\n";

Thanks again :smiley:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.