source: OGRE/trunk/ogrenew/RenderSystems/GL/src/GLSL/src/OgreGLSLLinkProgram.cpp @ 657

Revision 657, 7.3 KB checked in by mattausch, 18 years ago (diff)

added ogre dependencies and patched ogre sources

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25
26#include "OgreGLSLExtSupport.h"
27#include "OgreGpuProgram.h"
28#include "OgreGLSLLinkProgram.h"
29
30namespace Ogre {
31
32        //-----------------------------------------------------------------------
33
34        GLSLLinkProgram::GLSLLinkProgram(void) : mLinked(false),
35                mUniformRefsBuilt(false)
36        {
37                        checkForGLSLError( "GLSLLinkProgram::GLSLLinkProgram", "Error prior to Creating GLSL Program Object", 0 );
38                    mGLHandle = glCreateProgramObjectARB_ptr();
39                        checkForGLSLError( "GLSLLinkProgram::GLSLLinkProgram", "Error Creating GLSL Program Object", 0 );
40
41        }
42
43        //-----------------------------------------------------------------------
44        GLSLLinkProgram::~GLSLLinkProgram(void)
45        {
46                glDeleteObjectARB_ptr(mGLHandle);
47
48        }
49
50        //-----------------------------------------------------------------------
51        void GLSLLinkProgram::activate(void)
52        {
53                if (!mLinked)
54                {
55                        glLinkProgramARB_ptr( mGLHandle );
56                        glGetObjectParameterivARB_ptr( mGLHandle, GL_OBJECT_LINK_STATUS_ARB, &mLinked );
57                        // force logging and raise exception if not linked
58                        checkForGLSLError( "GLSLLinkProgram::Activate",
59                                "Error linking GLSL Program Object", mGLHandle, !mLinked, !mLinked );
60                        if(mLinked)
61                        {
62                                logObjectInfo( String("GLSL link result : "), mGLHandle );
63                                buildUniformReferences();
64                        }
65
66                }
67
68                if (mLinked)
69                {
70                    glUseProgramObjectARB_ptr( mGLHandle );
71                }
72        }
73
74        //-----------------------------------------------------------------------
75        void GLSLLinkProgram::buildUniformReferences(void)
76        {
77                if (!mUniformRefsBuilt)
78                {
79                        // scan through the active uniforms and add them to the reference list
80                        GLint    uniformCount;
81                        GLint  size;
82                        //GLenum type;
83                        #define BUFFERSIZE 100
84                        char   uniformName[BUFFERSIZE];
85                        //GLint location;
86                        UniformReference newUniformReference;
87
88                        // get the number of active uniforms
89                        glGetObjectParameterivARB_ptr(mGLHandle, GL_OBJECT_ACTIVE_UNIFORMS_ARB,
90                                        &uniformCount);
91
92                        // Loop over each of the active uniforms, and add them to the reference container
93                        // only do this for user defined uniforms, ignore built in gl state uniforms
94                        for (int index = 0; index < uniformCount; index++)
95                        {
96                                glGetActiveUniformARB_ptr(mGLHandle, index, BUFFERSIZE, NULL, &size, &newUniformReference.mType, uniformName);
97                                // don't add built in uniforms
98                                newUniformReference.mLocation = glGetUniformLocationARB_ptr(mGLHandle, uniformName);
99                                if (newUniformReference.mLocation >= 0)
100                                {
101                                        // user defined uniform found, add it to the reference list
102                                        newUniformReference.mName = String( uniformName );
103                                        // decode uniform size and type
104                                        switch (newUniformReference.mType)
105                                        {
106                                        case GL_FLOAT:
107                                                newUniformReference.isReal = true;
108                                                newUniformReference.mElementCount = 1;
109                                                break;
110
111                                        case GL_FLOAT_VEC2_ARB:
112                                                newUniformReference.isReal = true;
113                                                newUniformReference.mElementCount = 2;
114                                                break;
115
116                                        case GL_FLOAT_VEC3_ARB:
117                                                newUniformReference.isReal = true;
118                                                newUniformReference.mElementCount = 3;
119                                                break;
120
121                                        case GL_FLOAT_VEC4_ARB:
122                                                newUniformReference.isReal = true;
123                                                newUniformReference.mElementCount = 4;
124                                                break;
125
126                                        case GL_INT:
127                                        case GL_SAMPLER_1D_ARB:
128                                        case GL_SAMPLER_2D_ARB:
129                                        case GL_SAMPLER_3D_ARB:
130                                        case GL_SAMPLER_CUBE_ARB:
131                                                newUniformReference.isReal = false;
132                                                newUniformReference.mElementCount = 1;
133                                                break;
134
135                                        case GL_INT_VEC2_ARB:
136                                                newUniformReference.isReal = false;
137                                                newUniformReference.mElementCount = 2;
138                                                break;
139
140                                        case GL_INT_VEC3_ARB:
141                                                newUniformReference.isReal = false;
142                                                newUniformReference.mElementCount = 3;
143                                                break;
144
145                                        case GL_INT_VEC4_ARB:
146                                                newUniformReference.isReal = false;
147                                                newUniformReference.mElementCount = 4;
148                                                break;
149                                        }// end switch
150
151                                        mUniformReferences.push_back(newUniformReference);
152
153                                } // end if
154                        } // end for
155
156                        mUniformRefsBuilt = true;
157                }
158        }
159
160        //-----------------------------------------------------------------------
161        void GLSLLinkProgram::updateUniforms(GpuProgramParametersSharedPtr params)
162        {
163                // iterate through uniform reference list and update uniform values
164                UniformReferenceIterator currentUniform = mUniformReferences.begin();
165                UniformReferenceIterator endUniform = mUniformReferences.end();
166
167                GpuProgramParameters::RealConstantEntry* currentRealConstant;
168                GpuProgramParameters::IntConstantEntry* currentIntConstant;
169
170                while (currentUniform != endUniform)
171                {
172                        // get the index in the parameter real list
173
174                        if (currentUniform->isReal)
175                        {
176                                currentRealConstant = params->getNamedRealConstantEntry( currentUniform->mName );
177                                if (currentRealConstant != NULL)
178                                {
179                                        if (currentRealConstant->isSet)
180                                        {
181                                                switch (currentUniform->mElementCount)
182                                                {
183                                                case 1:
184                                                        glUniform1fvARB_ptr( currentUniform->mLocation, 1, currentRealConstant->val );
185                                                        break;
186
187                                                case 2:
188                                                        glUniform2fvARB_ptr( currentUniform->mLocation, 1, currentRealConstant->val );
189                                                        break;
190
191                                                case 3:
192                                                        glUniform3fvARB_ptr( currentUniform->mLocation, 1, currentRealConstant->val );
193                                                        break;
194
195                                                case 4:
196                                                        glUniform4fvARB_ptr( currentUniform->mLocation, 1, currentRealConstant->val );
197                                                        break;
198
199                                                } // end switch
200                                        }
201                                }
202                        }
203                        else
204                        {
205                                currentIntConstant = params->getNamedIntConstantEntry( currentUniform->mName );
206                                if (currentIntConstant != NULL)
207                                {
208                                        if (currentIntConstant->isSet)
209                                        {
210                                                switch (currentUniform->mElementCount)
211                                                {
212                                                case 1:
213                                                        glUniform1ivARB_ptr( currentUniform->mLocation, 1, (const GLint*)currentIntConstant->val );
214                                                        break;
215
216                                                case 2:
217                                                        glUniform2ivARB_ptr( currentUniform->mLocation, 1, (const GLint*)currentIntConstant->val );
218                                                        break;
219
220                                                case 3:
221                                                        glUniform3ivARB_ptr( currentUniform->mLocation, 1, (const GLint*)currentIntConstant->val );
222                                                        break;
223
224                                                case 4:
225                                                        glUniform4ivARB_ptr( currentUniform->mLocation, 1, (const GLint*)currentIntConstant->val );
226                                                        break;
227                                                } // end switch
228                                        }
229                                }
230
231                        }
232
233
234                        // get the next uniform
235                        ++currentUniform;
236
237                } // end while
238        }
239
240
241} // namespace Ogre
Note: See TracBrowser for help on using the repository browser.