ShaderProgram.cc 5.87 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

53
void ShaderProgram::setAttributeLocationsByVAO(ConstSharedVertexArrayObject _vao)
54
{
55
56
    // TODO: deprecate

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
    bool needsRelink = false;

    // search through all attributes of _vao
    VertexArrayObject::AttributeVec vaoAttributes = _vao->getAttributes();
    for(VertexArrayObject::AttributeVec::size_type i = 0; i < vaoAttributes.size(); ++i)
    {
        if(vaoAttributes[i].location == -1)
            continue;

        // find the name of the current attribute in its ArrayBuffer
        std::string arrayBufferAttributeName = vaoAttributes[i].arrayBuffer->getAttributes()[vaoAttributes[i].attributeID].name;

        // find out whether an attribute with a matching name exists in this ShaderProgram
        GLint attribLocation = getAttributeLocation(arrayBufferAttributeName);
        if(attribLocation != -1                         // attribute with that name exists?
        && attribLocation != vaoAttributes[i].location) // attribute with that name not already bound correctly?
        {
            bindAttributeLocation(arrayBufferAttributeName, vaoAttributes[i].location);

            needsRelink = true;
        }
    }

    // re-link the program only if necessary
    if(needsRelink)
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    {
        link();
        openGLRareError();
    }
}

void ShaderProgram::setFragmentDataLocationsByFBO(ConstSharedFrameBufferObject _fbo)
{
    // TODO: deprecate

    bool needsRelink = false;

    // search through all color attachments of _fbo
    FrameBufferObject::AttachmentVec fboAttachments = _fbo->getColorAttachments();
    for(FrameBufferObject::AttachmentVec::size_type i = 0; i < fboAttachments.size(); ++i)
    {
        // find out whether a fragment data location with a matching name exists in this shader
        GLint fragmentDataLocation = getFragmentDataLocation(fboAttachments[i].name);
Robert Menzel's avatar
Robert Menzel committed
100
101
        if (   fragmentDataLocation != -1           // fragment data location with that name exists?
            && fragmentDataLocation !=  (GLint) i)  // fragment data location with that name not already bound correctly?
102
103
104
105
106
107
108
109
110
111
        {
            bindFragmentDataLocation(fboAttachments[i].name, i);

            needsRelink = true;
        }
    }

    // re-link the program only if necessary
    if(needsRelink)
    {
112
        link();
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
        openGLRareError();
    }
}

void ShaderProgram::setAttributeLocations(ConstSharedLocationMappings _locationMappings)
{
    bool needsRelink = false;

    // search through all color attachments of _fbo
    LocationMappings::LocationMap locations = _locationMappings->getLocations();

    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 fragmentDataLocation = getAttributeLocation((*it).first);

        if(fragmentDataLocation != -1            // fragment data location with that name exists?
        && fragmentDataLocation != (*it).second) // fragment data location with that name not already bound correctly?
        {
            bindAttributeLocation((*it).first, (*it).second);

            needsRelink = true;
        }
    }

    // re-link the program only if necessary
    if(needsRelink)
    {
        link();
        openGLRareError();
    }
}

void ShaderProgram::setFragmentDataLocations(ConstSharedLocationMappings _locationMappings)
{
    bool needsRelink = false;

    // search through all color attachments of _fbo
    LocationMappings::LocationMap locations = _locationMappings->getLocations();

    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 fragmentDataLocation = getFragmentDataLocation((*it).first);

        if(fragmentDataLocation != -1            // fragment data location with that name exists?
        && fragmentDataLocation != (*it).second) // fragment data location with that name not already bound correctly?
        {
            bindFragmentDataLocation((*it).first, (*it).second);

            needsRelink = true;
        }
    }

    // re-link the program only if necessary
    if(needsRelink)
    {
        link();
        openGLRareError();
    }
177
}