ShaderProgram.cc 7.19 KB
Newer Older
Robert Menzel's avatar
Robert Menzel committed
1
2
3
4
5
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2011, Computer Graphics Group RWTH Aachen University         //
// All rights reserved.                                                       //
////////////////////////////////////////////////////////////////////////////////

Robert Menzel's avatar
Robert Menzel committed
6
#include <ACGL/OpenGL/Objects/ShaderProgram.hh>
7
#include <ACGL/OpenGL/Objects/VertexArrayObject.hh>
8
#include <ACGL/OpenGL/Objects/FrameBufferObject.hh>
Robert Menzel's avatar
Robert Menzel committed
9
#include <ACGL/OpenGL/Tools.hh>
Robert Menzel's avatar
Robert Menzel committed
10

Robert Menzel's avatar
Robert Menzel committed
11
12
using namespace ACGL::OpenGL;
using namespace ACGL::Base;
Robert Menzel's avatar
Robert Menzel committed
13
14
using namespace ACGL::Utils;

15
bool ShaderProgram::link() const
Robert Menzel's avatar
Robert Menzel committed
16
{
17
    glLinkProgram(mObjectName);
Robert Menzel's avatar
Robert Menzel committed
18

19
#ifdef ACGL_CHECK_CRITICAL_GL_ERRORS
Robert Menzel's avatar
Robert Menzel committed
20
21
    if ( openGLRareErrorOccured() )
    {
22
23
        // it's uncommon that glLinkProgram creates errors,
        // linking errors create no gl errors but a GL_LINK_STATUS of GL_FALSE
Robert Menzel's avatar
Robert Menzel committed
24
25
26
        return false;
    }

27
28
    // check for program link errors:
    GLint programError;
29
    glGetProgramiv(mObjectName, GL_LINK_STATUS, &programError);
Robert Menzel's avatar
Robert Menzel committed
30

31
    if (programError != GL_TRUE)
Robert Menzel's avatar
Robert Menzel committed
32
33
    {
        // yes, errors :-(
34
35
36
37
        error() << "Program could not get linked:" << std::endl;
    }

    GLsizei length = 0;
38
    glGetProgramiv(mObjectName, GL_INFO_LOG_LENGTH, &length);
39
40
41
42
    if (length > 1)
    {
        // error log or warnings:
        GLchar* pInfo = new char[length + 1];
43
        glGetProgramInfoLog(mObjectName,  length, &length, pInfo);
44
45
46
47
        openGLRareError();
        warning() << "Linker log: " << std::string(pInfo) << std::endl;
        delete[] pInfo;
        return (programError == GL_TRUE); // if the log contains only warnings we return true
Robert Menzel's avatar
Robert Menzel committed
48
    }
49
#endif
Robert Menzel's avatar
Robert Menzel committed
50
51
    return true;
}
52

Robert Menzel's avatar
Robert Menzel committed
53
void ShaderProgram::setAttributeLocations(ConstSharedLocationMappings _locationMappings)
54
55
56
{
    bool needsRelink = false;

Robert Menzel's avatar
Robert Menzel committed
57
58
    // search through all attributes:
    LocationMappings::LocationMap locations = _locationMappings->getLocations();
59

Robert Menzel's avatar
Robert Menzel committed
60
61
62
63
64
65
    for(LocationMappings::LocationMap::iterator it = locations.begin();
        it != locations.end();
        ++it)
    {
        // find out whether a fragment data location with a matching name exists in this shader
        GLint attributeLocation = getAttributeLocation((*it).first);
66

Robert Menzel's avatar
Robert Menzel committed
67
68
        if (            attributeLocation  != -1            // attributeLocation with that name exists?
            && ((GLuint)attributeLocation) != (*it).second) // attributeLocation with that name not already bound correctly?
69
        {
Robert Menzel's avatar
Robert Menzel committed
70
            bindAttributeLocation((*it).first, (*it).second);
71
72
73
74
75
76
            needsRelink = true;
        }
    }

    // re-link the program only if necessary
    if(needsRelink)
77
78
79
80
81
82
    {
        link();
        openGLRareError();
    }
}

Robert Menzel's avatar
Robert Menzel committed
83
void ShaderProgram::setFragmentDataLocations(ConstSharedLocationMappings _locationMappings)
84
85
86
{
    bool needsRelink = false;

Robert Menzel's avatar
Robert Menzel committed
87
88
89
90
91
92
    // search through all color attachments:
    LocationMappings::LocationMap locations = _locationMappings->getLocations();

    for(LocationMappings::LocationMap::iterator it = locations.begin();
        it != locations.end();
        ++it)
93
94
    {
        // find out whether a fragment data location with a matching name exists in this shader
Robert Menzel's avatar
Robert Menzel committed
95
        GLint fragmentDataLocation = getFragmentDataLocation((*it).first);
96

Robert Menzel's avatar
Robert Menzel committed
97
98
        //ACGL::Utils::debug() << "test ("<<(*it).first<<", "<<(*it).second<<"); loc:"<<fragmentDataLocation<<std::endl;

Robert Menzel's avatar
Robert Menzel committed
99
100
101
102
        if (             fragmentDataLocation  != -1            // fragment data location with that name exists?
            && ((GLuint) fragmentDataLocation) != (*it).second) // fragment data location with that name not already bound correctly?
        {
            bindFragmentDataLocation((*it).first, (*it).second);
Robert Menzel's avatar
Robert Menzel committed
103
            //ACGL::Utils::debug() << "bindFragmentDataLocation("<<(*it).first<<", "<<(*it).second<<");"<<std::endl;
104
105
106
107
108
109
110
            needsRelink = true;
        }
    }

    // re-link the program only if necessary
    if(needsRelink)
    {
111
        link();
112
113
114
115
        openGLRareError();
    }
}

Robert Menzel's avatar
Robert Menzel committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
SharedLocationMappings ShaderProgram::getAttributeLocations()
{
    SharedLocationMappings locationMap = SharedLocationMappings( new LocationMappings() );

    // query the number of _active_ attributes:
    GLint attributeCount;
    glGetProgramiv( mObjectName, GL_ACTIVE_ATTRIBUTES, &attributeCount );
    if (attributeCount == 0) return locationMap;

    // reserve a string long enought for the longest name:
    GLint longestAttributeName;
    glGetProgramiv( mObjectName, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH,  &longestAttributeName );
    char *name = new char[longestAttributeName+1];

    // get the name and location of each active attribute:
    for (int i = 0; i < attributeCount; ++i) {
        GLenum  type;
        GLint   size;
        GLsizei length;
        glGetActiveAttrib( mObjectName, i, longestAttributeName, &length, &size, &type, name );
Robert Menzel's avatar
Robert Menzel committed
136
        name[ length+1 ] = 0; // null terminate string
Robert Menzel's avatar
Robert Menzel committed
137
138
139
140
141
142

        GLint attribLocation = glGetAttribLocation( mObjectName, name );

        locationMap->setLocation( std::string(name), (GLuint) attribLocation );
    }

Robert Menzel's avatar
Robert Menzel committed
143
    delete[] name;
Robert Menzel's avatar
Robert Menzel committed
144
145
146
147
148
149
150
151
152
153
154
    return locationMap;
}

SharedLocationMappings ShaderProgram::getFragmentDataLocations()
{
    SharedLocationMappings locationMap = SharedLocationMappings( new LocationMappings() );

    ACGL::Utils::error() << " ShaderProgram::getFragmentDataLocations is not implemented -> missing OpenGL API" << std::endl;

    return locationMap;
}
Robert Menzel's avatar
Robert Menzel committed
155

Robert Menzel's avatar
Robert Menzel committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
SharedLocationMappings ShaderProgram::getUniformOffsetsOfBlock( GLuint _blockIndex )
{
    SharedLocationMappings locationMap = SharedLocationMappings( new LocationMappings() );

    if (_blockIndex == GL_INVALID_INDEX) return locationMap; // block does not exist

    // query the number of _active_ uniforms:
    GLint uniformCount;
    glGetProgramiv( mObjectName, GL_ACTIVE_UNIFORMS, &uniformCount );
    if (uniformCount == 0) return locationMap;

    // reserve a string long enought for the longest name:
    GLint longestUniformName;
    glGetProgramiv( mObjectName, GL_ACTIVE_UNIFORM_MAX_LENGTH,  &longestUniformName );
    char *name = new char[longestUniformName+1];

    // get the name and location of each active attribute:
    for (int i = 0; i < uniformCount; ++i) {
        GLsizei length;
        glGetActiveUniformName( mObjectName, i, longestUniformName, &length, name );

        name[ length+1 ] = 0; // null terminate string

        GLuint idx = i;
        GLint uniformBlockIndex;
        glGetActiveUniformsiv( mObjectName, 1, &idx, GL_UNIFORM_BLOCK_INDEX, &uniformBlockIndex );

        if (uniformBlockIndex != -1) {
            if ((GLuint)uniformBlockIndex == _blockIndex) {
                GLint offset;
                glGetActiveUniformsiv( mObjectName, 1, &idx, GL_UNIFORM_OFFSET, &offset );
                //ACGL::Utils::message() << "uniform " << i << " is " << name << " block: " << uniformBlockIndex << " offset: " << offset << std::endl;
                locationMap->setLocation( std::string(name), (GLuint) offset );
            }
        }
    }

    delete[] name;
    return locationMap;
}

GLsizeiptr ShaderProgram::getUniformBlockSize( GLuint _blockIndex )
{
    if (_blockIndex == GL_INVALID_INDEX) return 0; // block does not exist

    GLint uniformBlockSize;
    glGetActiveUniformBlockiv( mObjectName, _blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, &uniformBlockSize);

    return (GLsizeiptr) uniformBlockSize;
}