简体   繁体   English

无法使用QGLShaderProgram在Qt 4.7应用程序中使用组数组编译GLSL 3.30着色器

[英]Cannot compile GLSL 3.30 shader with group array in Qt 4.7 application using QGLShaderProgram

I'm developing an application that uses Qt 4.7 with the goal of creating a simple 3D OpenGL viewer. 我正在开发一个使用Qt 4.7的应用程序,目的是创建一个简单的3D OpenGL查看器。 I want to use GLSL 3.30 shaders. 我想使用GLSL 3.30着色器。 I use Linux Ubutu 11.10, my CG is a NVIDIA with a NVS 3100M GPU, the NVIDIA driver version is 280.13 and OpenGL version is 3.3.0 我使用Linux Ubutu 11.10,我的CG是具有NVS 3100M GPU的NVIDIA,NVIDIA驱动程序版本为280.13,OpenGL版本为3.3.0。

I have a working basic shader which doesn't compute any lighting. 我有一个可以工作的基本着色器,它不会计算任何光照。 That is wanted to do next but I'm stuck with a problem. 下一步需要这样做,但是我遇到了问题。

Here is my not-working modified vertex shader. 这是我无法使用的修改后的顶点着色器。

#version 330

in vec3 inPosition;
in vec4 inColor;
in vec3 inNormal;
uniform mat4 inModelMatrix;
uniform mat4 inViewMatrix;
uniform mat4 inProjectionMatrix;
out vec4 exColor;

uniform Light {
    vec4 position;
    vec4 ambient;
    vec4 diffuse;
    vec4 specular;
} lights[4];

void main(void)
{
    gl_Position = inProjectionMatrix * inViewMatrix * inModelMatrix * vec4(inPosition, 1.0);
    exColor = lights[0].ambient;
}

The OpenGL Shading Language reference for GLSL 3.30 states that: GLSL 3.30的OpenGL着色语言参考指出:

uniform Transform { // API uses “Transform[2]” to refer to instance 2
    mat4 ModelViewMatrix;
    mat4 ModelViewProjectionMatrix;
    float Deformation;
} transforms[4];
...
... = transforms[2].ModelViewMatrix; // shader access of instance 2
// API uses “Transform.ModelViewMatrix” to query an offset or other query

So if I'm not mistaken, the code is correct. 因此,如果我没记错的话,代码是正确的。 However, it doesn't even compile. 但是,它甚至不编译。 The following error is thrown: 引发以下错误:

QGLShader::link: "Vertex info
-----------
Internal error: assembly compile error for vertex shader at offset 1805:
-- error message --
line 38, column 15:  error: expected '='
line 82, column 36:  error: expected ';'
-- internal assembly text --
!!NVvp4.1
OPTION NV_parameter_buffer_object2;
# cgc version 3.1.0001, build date Jul 27 2011
# command line args: 
#vendor NVIDIA Corporation
#version 3.1.0.1
#profile gp4_1vp
#program main
#semantic Light.lights
#semantic inModelMatrix
#semantic inViewMatrix
#semantic inProjectionMatrix
#var float4 gl_Position : $vout.POSITION : HPOS : -1 : 1
#var float4 lights[0].position : BUFFER[0] : buffer[0][0] : -1 : 0
#var float4 lights[0].ambient : BUFFER[0] : buffer[0][16] : -1 : 1
#var float4 lights[0].diffuse : BUFFER[0] : buffer[0][32] : -1 : 0
#var float4 lights[0].specular : BUFFER[0] : buffer[0][48] : -1 : 0
#var float4 lights[1].position : BUFFER[1] : buffer[1][0] : -1 : 0
#var float4 lights[1].ambient : BUFFER[1] : buffer[1][16] : -1 : 0
#var float4 lights[1].diffuse : BUFFER[1] : buffer[1][32] : -1 : 0
#var float4 lights[1].specular : BUFFER[1] : buffer[1][48] : -1 : 0
#var float4 lights[2].position : BUFFER[2] : buffer[2][0] : -1 : 0
#var float4 lights[2].ambient : BUFFER[2] : buffer[2][16] : -1 : 0
#var float4 lights[2].diffuse : BUFFER[2] : buffer[2][32] : -1 : 0
#var float4 lights[2].specular : BUFFER[2] : buffer[2][48] : -1 : 0
#var float4 lights[3].position : BUFFER[3] : buffer[3][0] : -1 : 0
#var float4 lights[3].ambient : BUFFER[3] : buffer[3][16] : -1 : 0
#var float4 lights[3].diffuse : BUFFER[3] : buffer[3][32] : -1 : 0
#var float4 lights[3].specular : BUFFER[3] : buffer[3][48] : -1 : 0
#var float3 inPosition : $vin.ATTR0 : ATTR0 : -1 : 1
#var float4 inColor :  :  : -1 : 0
#var float3 inNormal :  :  : -1 : 0
#var float4x4 inModelMatrix :  : c[0], 4 : -1 : 1
#var float4x4 inViewMatrix :  : c[4], 4 : -1 : 1
#var float4x4 inProjectionMatrix :  : c[8], 4 : -1 : 1
#var float4 exColor : $vout.ATTR0 : ATTR0 : -1 : 1
PARAM c[12] = { program.local[0..11] };
CBUFFER buf0[][] = { program.buffer[0..3] };
ATTRIB vertex_attrib[] = { vertex.attrib[0..0] };
OUTPUT result_attrib[] = { result.attrib[0..0] };
TEMP R0, R1, R2, R3, R4, R5, R6, R7, R8;
MOV.F R3, c[9];
MOV.F R2, c[8];
MUL.F R0, R3, c[5].y;
MOV.F R1, c[10];
MAD.F R0, R2, c[5].x, R0;
MAD.F R5, R1, c[5].z, R0;
MOV.F R0, c[11];
MAD.F R6, R0, c[5].w, R5;
MUL.F R4, R3, c[4].y;
MAD.F R5, R2, c[4].x, R4;
MAD.F R5, R1, c[4].z, R5;
MAD.F R5, R0, c[4].w, R5;
MUL.F R4, R6, c[1].y;
MAD.F R8, R5, c[1].x, R4;
MUL.F R4, R3, c[6].y;
M    UL.F R7, R6, c[0].y;
MAD.F R4, R2, c[6].x, R4;
MUL.F R3, R3, c[7].y;
MAD.F R2, R2, c[7].x, R3;
MAD.F R3, R1, c[6].z, R4;
MAD.F R1, R1, c[7].z, R2;
MAD.F R2, R0, c[6].w, R3;
MUL.F R4, R6, c[2].y;
MAD.F R0, R0, c[7].w, R1;
MAD.F R3, R2, c[1].z, R8;
MAD.F R1, R0, c[1].w, R3;
MAD.F R7, R5, c[0].x, R7;
MAD.F R3, R2, c[0].z, R7;
MAD.F R3, R0, c[0].w, R3;
MUL.F R1, vertex.attrib[0].y, R1;
MAD.F R1, vertex.attrib[0].x, R3, R1;
MUL.F R3, R6, c[3].y;
MAD.F R3, R5, c[3].x, R3;
MAD.F R3, R2, c[3].z, R3;
MAD.F R4, R5, c[2].x, R4;
MAD.F R3, R0, c[3].w, R3;
MAD.F R2, R2, c[2].z, R4;
MAD.F R0, R0, c[2].w, R2;
MAD.F R0, vertex.attrib[0].z, R0, R1;
ADD.F result.position, R0, R3;
LDC.F32X4 result.attrib[0], buf0[0][16];
END
# 41 instructions, 9 R-regs
" 

If I remove the call to lights[0].ambient, the shader compiles but when I try to get the uniform location, it doesn't work: 如果删除对lights [0] .ambient的调用,则将编译着色器,但是当我尝试获取统一位置时,它将无法正常工作:

int lightsLocation = shaderProgram.uniformLocation("Light[0]");

PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)(context()->getProcAddress("glGetUniformLocation"));

qDebug() << lightsLocation << glGetUniformLocation(shaderProgram.programId(), "Light[0]"); // -1 -1

I've also tried this with "Light", "lights", "lights[0]", "lights[0].ambient" as uniform name with no success. 我也尝试使用“ Light”,“ lights”,“ lights [0]”,“ lights [0] .ambient”作为统一名称,但均未成功。

After some search, I've found this post: Setting the values of a struct array from JS to GLSL and thus tried to use structures as it is shown but this doesn't work either. 经过一番搜索,我发现了这篇文章: 设置从JS到GLSL的结构数组的值,因此尝试使用显示的结构,但这也不起作用。

Subsequent question 后续问题

Event if I could bring that to work thanks to some answer, I don't know which method of QGLShaderProgram I should use to upload the data. 如果我能通过一些回答使它生效,我不知道我应该使用哪种QGLShaderProgram方法来上传数据。

tl;dr tl; dr

1) Why doesn't the following code compile? 1)为什么以下代码无法编译? out vec4 exColor; 出vec4 exColor;

uniform Light {
     vec4 ambient;
} lights[4];

void main(void) {
    exColor = lights[0].ambient;
}

2) What uniform name should one use as parameter of QGLShaderProgam::uniformLocation() to get the location of a uniform Light { ... } lights[4]; 2)应该使用什么统一名称作为QGLShaderProgam :: uniformLocation()的参数来获得uniform Light { ... } lights[4]; or struct Light { ... }; uniform Light lights[4]; struct Light { ... }; uniform Light lights[4]; struct Light { ... }; uniform Light lights[4]; ?

3) Once the location is retrieved, what function of QGLShaderProgram should one use to upload the data to the GPU? 3)一旦检索到位置,应使用QGLShaderProgram的什么功能将数据上传到GPU?

Thank you very much! 非常感谢你!

1) You are missing "struct" before Light in order to be a structure. 1)为了成为结构,您在Light之前缺少“结构”。

or ... 要么 ...

Your code is legal, but it creates a Uniform Block , which is meant for easy sharing of large blocks of data between different shaders, and is not stored in shader uniform block but instead in a buffer object. 您的代码是合法的,但会创建一个Uniform Block ,这是为了轻松在不同着色器之间共享大数据块,并且不存储在着色器统一块中,而是存储在缓冲区对象中。 If you intended to do that, then you need to include the following line at the beginning of the shader: 如果您打算这样做,则需要在着色器的开头包括以下行:

#extension GL_ARB_uniform_buffer_object : enable

Note that this doesn't work with older NVIDIA GPUs (driver bug? it didn't work with older ATis either), but the same code works for me on GeForce GTX 560. 请注意,这不适用于较旧的NVIDIA GPU(驱动程序错误?也不适用于较旧的ATis),但是相同的代码对我在GeForce GTX 560上也适用。

Also, get familiar with GL_ARB_uniform_buffer_object (there is an example shader and code to set it up). 另外,熟悉GL_ARB_uniform_buffer_object (有示例着色器和代码来设置它)。

2) In case you are not using (referencing) a uniform, the compiler will optimize it away, that is why you can't get the uniform location. 2)如果您不使用(参考)制服,编译器会对其进行优化,这就是为什么您无法获得制服的位置。 That may lead to frustration because one can't get uniform location even if using the correct name. 这可能会导致沮丧,因为即使使用正确的名称也无法获得统一的位置。

For uniform buffer, the layout of the data in memory is given by the specs, you then create a buffer object (glGenBuffers()), fill it with raw data (glBufferData()) and bind it to the uniform block using glGetUniformBlockIndex() and glUniformBlockBinding(). 对于统一缓冲区,规范中给出了内存中数据的布局,然后创建缓冲区对象(glGenBuffers()),将其填充原始数据(glBufferData()),然后使用glGetUniformBlockIndex()将其绑定到统一块和glUniformBlockBinding()。

In case it was meant to be a "struct", you can always find names of active uniforms through glGetActiveUniformName() sou you can see what names to use. 如果要作为“结构”,则始终可以通过glGetActiveUniformName()找到活动制服的名称,这样您就可以看到要使用的名称。 For your structure, you would need to use "lights[0].ambient" through "lights[3].ambient" to get uniform locations, and then call glUniform4f() four times to specify the data (because the variables pointed to are vectors). 对于您的结构,您需要使用“ lights [0] .ambient”到“ lights [3] .ambient”来获得统一的位置,然后四次调用glUniform4f()来指定数据(因为指向的变量是向量)。

3) already said that above. 3)上面已经说过了。 either glBufferData() for uniform block, or appropriate glUniform*() for uniforms. 对于统一块,可以使用glBufferData(),对于统一块可以使用适当的glUniform *()。 if in structure, you need to upload structure members separately. 如果在结构中,则需要分别上传结构成员。 if in array of structures, still you need to upload structure members separately, for each structure in the array. 如果在结构数组中,仍然需要针对数组中的每个结构分别上传结构成员。 if an array is a member of structure, only then you would use glUniform*v() to upload an array of floats/vectors/matrices. 如果数组是结构的成员,则只有使用glUniform * v()才能上载浮点数/向量/矩阵的数组。

To handle the Uniform Buffer Objects in a similar way as Qt does for the Vertex Buffer Objects and Index Buffer Objects, I created this class: 为了以与Qt对“顶点缓冲区”对象和“索引缓冲区”对象类似的方式处理统一缓冲区对象,我创建了此类:

#ifndef UNIFORMBUFFEROBJECT_H
#define UNIFORMBUFFEROBJECT_H

#include <qgl.h>

struct UBOInfoStruct {
    QString uniformName;
    GLuint progId;
    GLuint blockIdx;
    GLint blockSize;
    GLuint bindingIdx;
};

typedef UBOInfoStruct UBOInfo;

class UniformBufferObject
{
public:
    UniformBufferObject();

    bool create();
    bool isCreated() const;

    void destroy();

    bool bind();
    bool bind(GLuint progId, QString uniformName);
    void release();

    GLuint bufferId() const;

//    bool read(int offset, void *data, int count);
    void write(int offset, const void *data, int count);

    void allocate(const void *data, int count);
    inline void allocate(int count) { allocate(0, count); }


protected:
    static GLuint bindingIndex();
    static QVector<GLuint> m_bindingIndices;

    bool initFunctionPointers(const QGLContext* m_glContext);

    GLuint m_bufferId;

    const QGLContext* m_glContext;

    bool m_inited;

    QVector<UBOInfo> m_UBOInfos;

    PFNGLBINDBUFFERPROC glBindBuffer;
    PFNGLBINDBUFFERBASEPROC glBindBufferBase;
    PFNGLBINDBUFFERRANGEPROC glBindBufferRange;
    PFNGLBUFFERDATAPROC glBufferData;
    PFNGLBUFFERSUBDATAPROC glBufferSubData;
    PFNGLDELETEBUFFERSPROC glDeleteBuffers;
    PFNGLGENBUFFERSPROC glGenBuffers;
    PFNGLGETACTIVEUNIFORMBLOCKIVPROC glGetActiveUniformBlockiv;
    PFNGLGETACTIVEUNIFORMSIVPROC glGetActiveUniformsiv;
    PFNGLGETUNIFORMBLOCKINDEXPROC glGetUniformBlockIndex;
    PFNGLGETUNIFORMINDICESPROC glGetUniformIndices;
    PFNGLUNIFORMBLOCKBINDINGPROC glUniformBlockBinding;
};

#endif // UNIFORMBUFFEROBJECT_H

CPP: CPP:

#include "uniformbufferobject.h"

#include <QGLContext>

#include <QDebug>

QVector<GLuint> UniformBufferObject::m_bindingIndices = QVector<GLuint>();

GLuint UniformBufferObject::bindingIndex() {
    for(GLuint i = 0, size = m_bindingIndices.size(); i < size; i++) {
        if(m_bindingIndices.at(i) != i) {
            m_bindingIndices.insert(i, i);
            return i;
        }
    }
    m_bindingIndices.append(m_bindingIndices.size());
    return m_bindingIndices.size();
}

UniformBufferObject::UniformBufferObject() :
    m_bufferId(0), m_glContext(NULL), m_inited(false),
    glBindBuffer(NULL), glBindBufferBase(NULL), glBindBufferRange(NULL), glBufferData(NULL), glDeleteBuffers(NULL), glGenBuffers(NULL),
    glGetActiveUniformBlockiv(NULL), glGetActiveUniformsiv(NULL), glGetUniformBlockIndex(NULL), glGetUniformIndices(NULL),
    glUniformBlockBinding(NULL)
{
}

bool UniformBufferObject::create() {
    const QGLContext* m_glContext = QGLContext::currentContext();

    if(m_glContext) {
        if(!m_inited && !initFunctionPointers(m_glContext)) {
            qDebug("Cannot find Uniform Buffer Objects related functions");
            return false;
        }

        GLuint tmpBufferId = 0;
        glGenBuffers(1, &tmpBufferId);

        if(tmpBufferId) {
                m_bufferId = tmpBufferId;
            this->m_glContext = m_glContext;

            return true;
        } else {
            qDebug("Invalid buffer Id");
        }
    }

    qDebug("Could not retrieve buffer");

    return false;
}

bool UniformBufferObject::isCreated() const {
    return (bool)(m_bufferId != 0);
}

void UniformBufferObject::destroy() {
    if(m_bufferId != 0) {
        glDeleteBuffers(1, &m_bufferId);
    }
    m_bufferId = 0;
    m_glContext = NULL;
}

bool UniformBufferObject::bind() {
    if(!isCreated()) {
        qDebug("Buffer not created");
        return false;
    }

    glBindBuffer(GL_UNIFORM_BUFFER, m_bufferId);

    return true;
}

bool UniformBufferObject::bind(GLuint progId, QString uniformName) {
    GLuint tmpBlockIdx = glGetUniformBlockIndex(progId, uniformName.toUtf8());

    if(tmpBlockIdx == GL_INVALID_INDEX) {
        qDebug() << QString("Could not find block index of block named: %1").arg(uniformName);

        return false;
    }

    GLint tmpBlockSize;
    glGetActiveUniformBlockiv(progId, tmpBlockIdx, GL_UNIFORM_BLOCK_DATA_SIZE, &tmpBlockSize);

    GLuint tmpBindingIdx = bindingIndex();
    glUniformBlockBinding(progId, tmpBlockIdx, tmpBindingIdx);

    glBindBufferBase(GL_UNIFORM_BUFFER, tmpBindingIdx, m_bufferId);

    if(glGetError() == GL_INVALID_VALUE || glGetError() == GL_INVALID_ENUM) {
        qDebug() << "ERROR";
    }

    UBOInfo info;
    info.progId = progId;
    info.uniformName = uniformName;
    info.blockIdx = tmpBlockIdx;
    info.blockSize = tmpBlockSize;
    info.bindingIdx = tmpBindingIdx;

    m_UBOInfos.append(info);

    return true;
}

void UniformBufferObject::release() {
    glBindBuffer(GL_UNIFORM_BUFFER, 0);
}

GLuint UniformBufferObject::bufferId() const {
    return m_bufferId;
}

void UniformBufferObject::write(int offset, const void *data, int count) {
    if(!isCreated())
        return;

    bind();

    glBufferSubData(GL_UNIFORM_BUFFER, offset, count, data);
}

void UniformBufferObject::allocate(const void *data, int count) {
    if(!isCreated())
        return;

    bind();

    glBufferData(GL_UNIFORM_BUFFER, count, data, GL_DYNAMIC_DRAW);
}

bool UniformBufferObject::initFunctionPointers(const QGLContext* m_glContext) {
    glBindBuffer = (PFNGLBINDBUFFERPROC)m_glContext->getProcAddress("glBindBuffer");
    glBindBufferBase = (PFNGLBINDBUFFERBASEPROC)m_glContext->getProcAddress("glBindBufferBase");
    glBindBufferRange = (PFNGLBINDBUFFERRANGEPROC)m_glContext->getProcAddress("glBindBufferRange");
    glBufferData = (PFNGLBUFFERDATAPROC)m_glContext->getProcAddress("glBufferData");
    glBufferSubData = (PFNGLBUFFERSUBDATAPROC)m_glContext->getProcAddress("glBufferSubData");
    glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)m_glContext->getProcAddress("glDeleteBuffers");
    glGenBuffers = (PFNGLGENBUFFERSPROC)m_glContext->getProcAddress("glGenBuffers");
    glGetActiveUniformBlockiv = (PFNGLGETACTIVEUNIFORMBLOCKIVPROC)m_glContext->getProcAddress("glGetActiveUniformBlockiv");
    glGetActiveUniformsiv = (PFNGLGETACTIVEUNIFORMSIVPROC)m_glContext->getProcAddress("glGetActiveUniformsiv");
    glGetUniformBlockIndex = (PFNGLGETUNIFORMBLOCKINDEXPROC)m_glContext->getProcAddress("glGetUniformBlockIndex");
    glGetUniformIndices = (PFNGLGETUNIFORMINDICESPROC)m_glContext->getProcAddress("glGetUniformIndices");
    glUniformBlockBinding = (PFNGLUNIFORMBLOCKBINDINGPROC)m_glContext->getProcAddress("glUniformBlockBinding");

    if(!glBindBuffer ||
            !glBindBufferBase ||
            !glBindBufferRange ||
            !glBufferData ||
            !glBufferSubData ||
            !glDeleteBuffers ||
            !glGenBuffers ||
            !glGetActiveUniformBlockiv ||
            !glGetActiveUniformsiv ||
            !glGetUniformBlockIndex ||
            !glGetUniformIndices ||
            !glUniformBlockBinding)
    {
        qDebug("Could not init function pointers");
        return false;
    }

    return true;
}

As highlighted in the answer which I accepted, Uniform Buffer Objects have an associated layout ( http://www.opengl.org/wiki/Uniform_Buffer_Object ) and the Uniform Blocks have to be linked "twice" (read more here: http://arcsynthesis.org/gltut/Positioning/Tut07%20Shared%20Uniforms.html ). 正如我接受的答案中突出显示的那样,统一缓冲区对象具有关联的布局( http://www.opengl.org/wiki/Uniform_Buffer_Object ),并且“统一块”必须链接“两次”(在此处了解更多信息: http:/ /arcsynthesis.org/gltut/Positioning/Tut07%20Shared%20Uniforms.html )。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM