| 1 | /* | 
|---|
| 2 | ----------------------------------------------------------------------------- | 
|---|
| 3 | This source file is part of OGRE | 
|---|
| 4 | (Object-oriented Graphics Rendering Engine) | 
|---|
| 5 | For the latest info, see http://www.ogre3d.org/ | 
|---|
| 6 |  | 
|---|
| 7 | Copyright (c) 2000-2006 Torus Knot Software Ltd | 
|---|
| 8 | Also see acknowledgements in Readme.html | 
|---|
| 9 |  | 
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under | 
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software | 
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later | 
|---|
| 13 | version. | 
|---|
| 14 |  | 
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT | 
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. | 
|---|
| 18 |  | 
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with | 
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple | 
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to | 
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. | 
|---|
| 23 |  | 
|---|
| 24 | You may alternatively use this source under the terms of a specific version of | 
|---|
| 25 | the OGRE Unrestricted License provided you have obtained such a license from | 
|---|
| 26 | Torus Knot Software Ltd. | 
|---|
| 27 | ----------------------------------------------------------------------------- | 
|---|
| 28 | */ | 
|---|
| 29 |  | 
|---|
| 30 | #include "OgreGLGpuProgram.h" | 
|---|
| 31 | #include "OgreException.h" | 
|---|
| 32 | #include "OgreStringConverter.h" | 
|---|
| 33 |  | 
|---|
| 34 | using namespace Ogre; | 
|---|
| 35 |  | 
|---|
| 36 | GLGpuProgram::GLGpuProgram(ResourceManager* creator, const String& name,  | 
|---|
| 37 |     ResourceHandle handle, const String& group, bool isManual,  | 
|---|
| 38 |     ManualResourceLoader* loader)  | 
|---|
| 39 |     : GpuProgram(creator, name, handle, group, isManual, loader) | 
|---|
| 40 | { | 
|---|
| 41 |     if (createParamDictionary("GLGpuProgram")) | 
|---|
| 42 |     { | 
|---|
| 43 |         setupBaseParamDictionary(); | 
|---|
| 44 |     } | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | GLGpuProgram::~GLGpuProgram() | 
|---|
| 48 | { | 
|---|
| 49 |     // have to call this here reather than in Resource destructor | 
|---|
| 50 |     // since calling virtual methods in base destructors causes crash | 
|---|
| 51 |     unload();  | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | GLuint GLGpuProgram::getAttributeIndex(VertexElementSemantic semantic) | 
|---|
| 55 | { | 
|---|
| 56 |         // default implementation | 
|---|
| 57 |         switch(semantic) | 
|---|
| 58 |         { | 
|---|
| 59 |                 case VES_POSITION: | 
|---|
| 60 |                 case VES_NORMAL: | 
|---|
| 61 |                 case VES_DIFFUSE: | 
|---|
| 62 |                 case VES_SPECULAR: | 
|---|
| 63 |                 case VES_TEXTURE_COORDINATES: | 
|---|
| 64 |                         assert(false && "Shouldn't be calling this for standard attributes!"); | 
|---|
| 65 |                         break; | 
|---|
| 66 |                 case VES_BLEND_INDICES: | 
|---|
| 67 |                         return 7; // default binding | 
|---|
| 68 |                 case VES_BLEND_WEIGHTS: | 
|---|
| 69 |                         return 1; // default binding | 
|---|
| 70 |                 case VES_TANGENT: | 
|---|
| 71 |                         return 14; // default binding | 
|---|
| 72 |                 case VES_BINORMAL: | 
|---|
| 73 |                         return 15; // default binding | 
|---|
| 74 |         } | 
|---|
| 75 |  | 
|---|
| 76 |         return 0; | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | bool GLGpuProgram::isAttributeValid(VertexElementSemantic semantic) | 
|---|
| 80 | { | 
|---|
| 81 |         // default implementation | 
|---|
| 82 |         switch(semantic) | 
|---|
| 83 |         { | 
|---|
| 84 |                 case VES_POSITION: | 
|---|
| 85 |                 case VES_NORMAL: | 
|---|
| 86 |                 case VES_DIFFUSE: | 
|---|
| 87 |                 case VES_SPECULAR: | 
|---|
| 88 |                 case VES_TEXTURE_COORDINATES: | 
|---|
| 89 |                         assert(false && "Shouldn't be calling this for standard attributes!"); | 
|---|
| 90 |                         break; | 
|---|
| 91 |                 case VES_BLEND_WEIGHTS: | 
|---|
| 92 |                 case VES_BLEND_INDICES: | 
|---|
| 93 |                 case VES_BINORMAL: | 
|---|
| 94 |                 case VES_TANGENT: | 
|---|
| 95 |                         return true; // with default binding | 
|---|
| 96 |         } | 
|---|
| 97 |  | 
|---|
| 98 |     return false; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | GLArbGpuProgram::GLArbGpuProgram(ResourceManager* creator, const String& name,  | 
|---|
| 102 |     ResourceHandle handle, const String& group, bool isManual,  | 
|---|
| 103 |     ManualResourceLoader* loader)  | 
|---|
| 104 |     : GLGpuProgram(creator, name, handle, group, isManual, loader) | 
|---|
| 105 | { | 
|---|
| 106 |     glGenProgramsARB(1, &mProgramID); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 | GLArbGpuProgram::~GLArbGpuProgram() | 
|---|
| 110 | { | 
|---|
| 111 |     // have to call this here reather than in Resource destructor | 
|---|
| 112 |     // since calling virtual methods in base destructors causes crash | 
|---|
| 113 |     unload();  | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | void GLArbGpuProgram::setType(GpuProgramType t) | 
|---|
| 117 | { | 
|---|
| 118 |     GLGpuProgram::setType(t); | 
|---|
| 119 |     mProgramType = (mType == GPT_VERTEX_PROGRAM) ? GL_VERTEX_PROGRAM_ARB : GL_FRAGMENT_PROGRAM_ARB; | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | void GLArbGpuProgram::bindProgram(void) | 
|---|
| 123 | { | 
|---|
| 124 |     glEnable(mProgramType); | 
|---|
| 125 |     glBindProgramARB(mProgramType, mProgramID); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | void GLArbGpuProgram::unbindProgram(void) | 
|---|
| 129 | { | 
|---|
| 130 |     glBindProgramARB(mProgramType, 0); | 
|---|
| 131 |     glDisable(mProgramType); | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | void GLArbGpuProgram::bindProgramParameters(GpuProgramParametersSharedPtr params) | 
|---|
| 135 | { | 
|---|
| 136 |     GLenum type = (mType == GPT_VERTEX_PROGRAM) ?  | 
|---|
| 137 |         GL_VERTEX_PROGRAM_ARB : GL_FRAGMENT_PROGRAM_ARB; | 
|---|
| 138 |      | 
|---|
| 139 |         // only supports float constants | 
|---|
| 140 |         const GpuLogicalBufferStruct* floatStruct = params->getFloatLogicalBufferStruct(); | 
|---|
| 141 |  | 
|---|
| 142 |         for (GpuLogicalIndexUseMap::const_iterator i = floatStruct->map.begin(); | 
|---|
| 143 |                 i != floatStruct->map.end(); ++i) | 
|---|
| 144 |         { | 
|---|
| 145 |                 size_t logicalIndex = i->first; | 
|---|
| 146 |                 const float* pFloat = params->getFloatPointer(i->second.physicalIndex); | 
|---|
| 147 |                 // Iterate over the params, set in 4-float chunks (low-level) | 
|---|
| 148 |                 for (size_t j = 0; j < i->second.currentSize; j+=4) | 
|---|
| 149 |                 { | 
|---|
| 150 |                         glProgramLocalParameter4fvARB(type, logicalIndex, pFloat); | 
|---|
| 151 |                         pFloat += 4; | 
|---|
| 152 |                         ++logicalIndex; | 
|---|
| 153 |                 } | 
|---|
| 154 |         } | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | void GLArbGpuProgram::bindProgramPassIterationParameters(GpuProgramParametersSharedPtr params) | 
|---|
| 158 | { | 
|---|
| 159 |     if (params->hasPassIterationNumber()) | 
|---|
| 160 |     { | 
|---|
| 161 |                 GLenum type = (mType == GPT_VERTEX_PROGRAM) ?  | 
|---|
| 162 |                         GL_VERTEX_PROGRAM_ARB : GL_FRAGMENT_PROGRAM_ARB; | 
|---|
| 163 |  | 
|---|
| 164 |                 size_t physicalIndex = params->getPassIterationNumberIndex(); | 
|---|
| 165 |                 size_t logicalIndex = params->getFloatLogicalIndexForPhysicalIndex(physicalIndex); | 
|---|
| 166 |                 const float* pFloat = params->getFloatPointer(physicalIndex); | 
|---|
| 167 |         glProgramLocalParameter4fvARB(type, (GLuint)logicalIndex, pFloat); | 
|---|
| 168 |     } | 
|---|
| 169 |  | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | void GLArbGpuProgram::unloadImpl(void) | 
|---|
| 173 | { | 
|---|
| 174 |     glDeleteProgramsARB(1, &mProgramID); | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | void GLArbGpuProgram::loadFromSource(void) | 
|---|
| 178 | { | 
|---|
| 179 |     glBindProgramARB(mProgramType, mProgramID); | 
|---|
| 180 |     glProgramStringARB(mProgramType, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)mSource.length(), mSource.c_str()); | 
|---|
| 181 |  | 
|---|
| 182 |     if (GL_INVALID_OPERATION == glGetError()) | 
|---|
| 183 |     { | 
|---|
| 184 |         GLint errPos; | 
|---|
| 185 |         glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos); | 
|---|
| 186 |                 String errPosStr = StringConverter::toString(errPos); | 
|---|
| 187 |         char* errStr = (char*)glGetString(GL_PROGRAM_ERROR_STRING_ARB); | 
|---|
| 188 |         // XXX New exception code? | 
|---|
| 189 |         OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,  | 
|---|
| 190 |             "Cannot load GL vertex program " + mName +  | 
|---|
| 191 |             ".  Line " + errPosStr + ":\n" + errStr, mName); | 
|---|
| 192 |     } | 
|---|
| 193 |     glBindProgramARB(mProgramType, 0); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|