2
0

CmGLSLGpuProgram.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 "CmException.h"
  25. #include "CmGLSLExtSupport.h"
  26. #include "CmGLSLGpuProgram.h"
  27. #include "CmGLSLLinkProgramManager.h"
  28. #include "CmGLSLLinkProgram.h"
  29. #include "CmGLSLProgram.h"
  30. namespace CamelotEngine {
  31. GLuint GLSLGpuProgram::mVertexShaderCount = 0;
  32. GLuint GLSLGpuProgram::mFragmentShaderCount = 0;
  33. GLuint GLSLGpuProgram::mGeometryShaderCount = 0;
  34. //-----------------------------------------------------------------------------
  35. GLSLGpuProgram::GLSLGpuProgram(GLSLProgram* parent) :
  36. GLGpuProgram(), mGLSLProgram(parent)
  37. {
  38. mType = parent->getType();
  39. mSyntaxCode = "glsl";
  40. if (parent->getType() == GPT_VERTEX_PROGRAM)
  41. {
  42. mProgramID = ++mVertexShaderCount;
  43. }
  44. else if (parent->getType() == GPT_FRAGMENT_PROGRAM)
  45. {
  46. mProgramID = ++mFragmentShaderCount;
  47. }
  48. else
  49. {
  50. mProgramID = ++mGeometryShaderCount;
  51. }
  52. }
  53. //-----------------------------------------------------------------------
  54. GLSLGpuProgram::~GLSLGpuProgram()
  55. {
  56. // have to call this here reather than in Resource destructor
  57. // since calling virtual methods in base destructors causes crash
  58. unload();
  59. }
  60. //-----------------------------------------------------------------------------
  61. void GLSLGpuProgram::loadImpl(void)
  62. {
  63. // nothing to load
  64. }
  65. //-----------------------------------------------------------------------------
  66. void GLSLGpuProgram::unloadImpl(void)
  67. {
  68. // nothing to unload
  69. }
  70. //-----------------------------------------------------------------------------
  71. void GLSLGpuProgram::loadFromSource(void)
  72. {
  73. // nothing to load
  74. }
  75. //-----------------------------------------------------------------------------
  76. void GLSLGpuProgram::bindProgram(void)
  77. {
  78. // tell the Link Program Manager what shader is to become active
  79. switch (mType)
  80. {
  81. case GPT_VERTEX_PROGRAM:
  82. GLSLLinkProgramManager::getSingleton().setActiveVertexShader( this );
  83. break;
  84. case GPT_FRAGMENT_PROGRAM:
  85. GLSLLinkProgramManager::getSingleton().setActiveFragmentShader( this );
  86. break;
  87. case GPT_GEOMETRY_PROGRAM:
  88. GLSLLinkProgramManager::getSingleton().setActiveGeometryShader( this );
  89. break;
  90. }
  91. }
  92. //-----------------------------------------------------------------------------
  93. void GLSLGpuProgram::unbindProgram(void)
  94. {
  95. // tell the Link Program Manager what shader is to become inactive
  96. if (mType == GPT_VERTEX_PROGRAM)
  97. {
  98. GLSLLinkProgramManager::getSingleton().setActiveVertexShader( NULL );
  99. }
  100. else if (mType == GPT_GEOMETRY_PROGRAM)
  101. {
  102. GLSLLinkProgramManager::getSingleton().setActiveGeometryShader( NULL );
  103. }
  104. else // its a fragment shader
  105. {
  106. GLSLLinkProgramManager::getSingleton().setActiveFragmentShader( NULL );
  107. }
  108. }
  109. //-----------------------------------------------------------------------------
  110. void GLSLGpuProgram::bindProgramParameters(GpuProgramParametersSharedPtr params, UINT16 mask)
  111. {
  112. // activate the link program object
  113. GLSLLinkProgram* linkProgram = GLSLLinkProgramManager::getSingleton().getActiveLinkProgram();
  114. // pass on parameters from params to program object uniforms
  115. linkProgram->updateUniforms(params, mask, mType);
  116. }
  117. //-----------------------------------------------------------------------------
  118. void GLSLGpuProgram::bindProgramPassIterationParameters(GpuProgramParametersSharedPtr params)
  119. {
  120. // activate the link program object
  121. GLSLLinkProgram* linkProgram = GLSLLinkProgramManager::getSingleton().getActiveLinkProgram();
  122. // pass on parameters from params to program object uniforms
  123. linkProgram->updatePassIterationUniforms( params );
  124. }
  125. //-----------------------------------------------------------------------------
  126. GLuint GLSLGpuProgram::getAttributeIndex(VertexElementSemantic semantic, UINT32 index)
  127. {
  128. // get link program - only call this in the context of bound program
  129. GLSLLinkProgram* linkProgram = GLSLLinkProgramManager::getSingleton().getActiveLinkProgram();
  130. if (linkProgram->isAttributeValid(semantic, index))
  131. {
  132. return linkProgram->getAttributeIndex(semantic, index);
  133. }
  134. else
  135. {
  136. // fall back to default implementation, allow default bindings
  137. return GLGpuProgram::getAttributeIndex(semantic, index);
  138. }
  139. }
  140. //-----------------------------------------------------------------------------
  141. bool GLSLGpuProgram::isAttributeValid(VertexElementSemantic semantic, UINT32 index)
  142. {
  143. // get link program - only call this in the context of bound program
  144. GLSLLinkProgram* linkProgram = GLSLLinkProgramManager::getSingleton().getActiveLinkProgram();
  145. if (linkProgram->isAttributeValid(semantic, index))
  146. {
  147. return true;
  148. }
  149. else
  150. {
  151. // fall back to default implementation, allow default bindings
  152. return GLGpuProgram::isAttributeValid(semantic, index);
  153. }
  154. }
  155. }