CmGLGpuProgram.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmGLGpuProgram.h"
  25. #include "OgreException.h"
  26. #include "OgreStringConverter.h"
  27. using namespace CamelotEngine;
  28. GLenum getGLShaderType(GpuProgramType programType)
  29. {
  30. switch (programType)
  31. {
  32. case GPT_VERTEX_PROGRAM:
  33. default:
  34. return GL_VERTEX_PROGRAM_ARB;
  35. case GPT_GEOMETRY_PROGRAM:
  36. return GL_GEOMETRY_PROGRAM_NV;
  37. case GPT_FRAGMENT_PROGRAM:
  38. return GL_FRAGMENT_PROGRAM_ARB;
  39. }
  40. }
  41. GLGpuProgram::GLGpuProgram()
  42. : GpuProgram()
  43. {
  44. //if (createParamDictionary("GLGpuProgram"))
  45. //{
  46. // setupBaseParamDictionary();
  47. //}
  48. }
  49. GLGpuProgram::~GLGpuProgram()
  50. {
  51. // have to call this here reather than in Resource destructor
  52. // since calling virtual methods in base destructors causes crash
  53. unload();
  54. }
  55. GLuint GLGpuProgram::getAttributeIndex(VertexElementSemantic semantic, CamelotEngine::UINT32 index)
  56. {
  57. return getFixedAttributeIndex(semantic, index);
  58. }
  59. GLuint GLGpuProgram::getFixedAttributeIndex(VertexElementSemantic semantic, CamelotEngine::UINT32 index)
  60. {
  61. // Some drivers (e.g. OS X on nvidia) incorrectly determine the attribute binding automatically
  62. // and end up aliasing existing built-ins. So avoid! Fixed builtins are:
  63. // a builtin custom attrib name
  64. // ----------------------------------------------
  65. // 0 gl_Vertex vertex
  66. // 1 n/a blendWeights
  67. // 2 gl_Normal normal
  68. // 3 gl_Color colour
  69. // 4 gl_SecondaryColor secondary_colour
  70. // 5 gl_FogCoord fog_coord
  71. // 7 n/a blendIndices
  72. // 8 gl_MultiTexCoord0 uv0
  73. // 9 gl_MultiTexCoord1 uv1
  74. // 10 gl_MultiTexCoord2 uv2
  75. // 11 gl_MultiTexCoord3 uv3
  76. // 12 gl_MultiTexCoord4 uv4
  77. // 13 gl_MultiTexCoord5 uv5
  78. // 14 gl_MultiTexCoord6 uv6, tangent
  79. // 15 gl_MultiTexCoord7 uv7, binormal
  80. switch(semantic)
  81. {
  82. case VES_POSITION:
  83. return 0;
  84. case VES_BLEND_WEIGHTS:
  85. return 1;
  86. case VES_NORMAL:
  87. return 2;
  88. case VES_DIFFUSE:
  89. return 3;
  90. case VES_SPECULAR:
  91. return 4;
  92. case VES_BLEND_INDICES:
  93. return 7;
  94. case VES_TEXTURE_COORDINATES:
  95. return 8 + index;
  96. case VES_TANGENT:
  97. return 14;
  98. case VES_BINORMAL:
  99. return 15;
  100. default:
  101. assert(false && "Missing attribute!");
  102. return 0;
  103. };
  104. }
  105. bool GLGpuProgram::isAttributeValid(VertexElementSemantic semantic, CamelotEngine::UINT32 index)
  106. {
  107. // default implementation
  108. switch(semantic)
  109. {
  110. case VES_POSITION:
  111. case VES_NORMAL:
  112. case VES_DIFFUSE:
  113. case VES_SPECULAR:
  114. case VES_TEXTURE_COORDINATES:
  115. return false;
  116. case VES_BLEND_WEIGHTS:
  117. case VES_BLEND_INDICES:
  118. case VES_BINORMAL:
  119. case VES_TANGENT:
  120. return true; // with default binding
  121. }
  122. return false;
  123. }
  124. GLArbGpuProgram::GLArbGpuProgram()
  125. : GLGpuProgram()
  126. {
  127. glGenProgramsARB(1, &mProgramID);
  128. }
  129. GLArbGpuProgram::~GLArbGpuProgram()
  130. {
  131. // have to call this here reather than in Resource destructor
  132. // since calling virtual methods in base destructors causes crash
  133. unload();
  134. }
  135. void GLArbGpuProgram::setType(GpuProgramType t)
  136. {
  137. GLGpuProgram::setType(t);
  138. mProgramType = getGLShaderType(t);
  139. }
  140. void GLArbGpuProgram::bindProgram(void)
  141. {
  142. glEnable(mProgramType);
  143. glBindProgramARB(mProgramType, mProgramID);
  144. }
  145. void GLArbGpuProgram::unbindProgram(void)
  146. {
  147. glBindProgramARB(mProgramType, 0);
  148. glDisable(mProgramType);
  149. }
  150. void GLArbGpuProgram::bindProgramParameters(GpuProgramParametersSharedPtr params, CamelotEngine::UINT16 mask)
  151. {
  152. GLenum type = getGLShaderType(mType);
  153. // only supports float constants
  154. GpuLogicalBufferStructPtr floatStruct = params->getFloatLogicalBufferStruct();
  155. for (GpuLogicalIndexUseMap::const_iterator i = floatStruct->map.begin();
  156. i != floatStruct->map.end(); ++i)
  157. {
  158. if (i->second.variability & mask)
  159. {
  160. size_t logicalIndex = i->first;
  161. const float* pFloat = params->getFloatPointer(i->second.physicalIndex);
  162. // Iterate over the params, set in 4-float chunks (low-level)
  163. for (size_t j = 0; j < i->second.currentSize; j+=4)
  164. {
  165. glProgramLocalParameter4fvARB(type, logicalIndex, pFloat);
  166. pFloat += 4;
  167. ++logicalIndex;
  168. }
  169. }
  170. }
  171. }
  172. void GLArbGpuProgram::bindProgramPassIterationParameters(GpuProgramParametersSharedPtr params)
  173. {
  174. if (params->hasPassIterationNumber())
  175. {
  176. GLenum type = getGLShaderType(mType);
  177. size_t physicalIndex = params->getPassIterationNumberIndex();
  178. size_t logicalIndex = params->getFloatLogicalIndexForPhysicalIndex(physicalIndex);
  179. const float* pFloat = params->getFloatPointer(physicalIndex);
  180. glProgramLocalParameter4fvARB(type, (GLuint)logicalIndex, pFloat);
  181. }
  182. }
  183. void GLArbGpuProgram::unloadImpl(void)
  184. {
  185. glDeleteProgramsARB(1, &mProgramID);
  186. }
  187. void GLArbGpuProgram::loadFromSource(void)
  188. {
  189. if (GL_INVALID_OPERATION == glGetError()) {
  190. // TODO LOG PORT LOG - Log this somewhere
  191. //LogManager::getSingleton().logMessage("Invalid Operation before loading program "+mName);
  192. }
  193. glBindProgramARB(mProgramType, mProgramID);
  194. glProgramStringARB(mProgramType, GL_PROGRAM_FORMAT_ASCII_ARB, (GLsizei)mSource.length(), mSource.c_str());
  195. if (GL_INVALID_OPERATION == glGetError())
  196. {
  197. GLint errPos;
  198. glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errPos);
  199. String errPosStr = StringConverter::toString(errPos);
  200. char* errStr = (char*)glGetString(GL_PROGRAM_ERROR_STRING_ARB);
  201. // XXX New exception code?
  202. OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR,
  203. "Cannot load GL vertex program. Line " + errPosStr + ":\n" + errStr, "");
  204. }
  205. glBindProgramARB(mProgramType, 0);
  206. }