OGLShaderProgram.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  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. #include "Precompiled.h"
  24. #include "Graphics.h"
  25. #include "GraphicsImpl.h"
  26. #include "ShaderProgram.h"
  27. #include "ShaderVariation.h"
  28. #include "DebugNew.h"
  29. ShaderProgram::ShaderProgram(Graphics* graphics, ShaderVariation* vertexShader, ShaderVariation* pixelShader) :
  30. GPUObject(graphics),
  31. vertexShader_(vertexShader),
  32. pixelShader_(pixelShader),
  33. lastParameterFrame_(0),
  34. linked_(false)
  35. {
  36. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  37. useTextureUnit_[i] = false;
  38. }
  39. ShaderProgram::~ShaderProgram()
  40. {
  41. Release();
  42. }
  43. void ShaderProgram::Release()
  44. {
  45. if (object_)
  46. {
  47. if (!graphics_)
  48. return;
  49. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  50. useTextureUnit_[i] = false;
  51. shaderParameters_.Clear();
  52. if (graphics_->GetShaderProgram() == this)
  53. graphics_->SetShaders(0, 0);
  54. glDeleteProgram(object_);
  55. object_ = 0;
  56. linked_ = false;
  57. linkerOutput_.Clear();
  58. }
  59. }
  60. bool ShaderProgram::Link()
  61. {
  62. Release();
  63. if (!vertexShader_ || !pixelShader_ || !vertexShader_->GetGPUObject() || !pixelShader_->GetGPUObject())
  64. return false;
  65. object_ = glCreateProgram();
  66. if (!object_)
  67. {
  68. linkerOutput_ = "Could not create shader program";
  69. return false;
  70. }
  71. glAttachShader(object_, vertexShader_->GetGPUObject());
  72. glAttachShader(object_, pixelShader_->GetGPUObject());
  73. glLinkProgram(object_);
  74. int linked, length;
  75. glGetProgramiv(object_, GL_LINK_STATUS, &linked);
  76. linked_ = linked != 0;
  77. if (!linked_)
  78. {
  79. glGetProgramiv(object_, GL_INFO_LOG_LENGTH, &length);
  80. linkerOutput_.Resize(length);
  81. glGetProgramInfoLog(object_, length, &length, &linkerOutput_[0]);
  82. }
  83. else
  84. linkerOutput_.Clear();
  85. if (!linked_)
  86. return false;
  87. const int MAX_PARAMETER_NAME_LENGTH = 256;
  88. char uniformName[MAX_PARAMETER_NAME_LENGTH];
  89. int uniformCount;
  90. glUseProgram(object_);
  91. glGetProgramiv(object_, GL_ACTIVE_UNIFORMS, &uniformCount);
  92. // Check for shader parameters and texture units
  93. for (int i = 0; i < uniformCount; ++i)
  94. {
  95. int location;
  96. unsigned type;
  97. int count;
  98. glGetActiveUniform(object_, i, MAX_PARAMETER_NAME_LENGTH, 0, &count, &type, uniformName);
  99. location = glGetUniformLocation(object_, uniformName);
  100. // Skip inbuilt or disabled uniforms
  101. if (location < 0)
  102. continue;
  103. // Check for array index included in the name and strip it
  104. String name(uniformName);
  105. unsigned index = name.Find('[');
  106. if (index != String::NPOS)
  107. {
  108. // If not the first index, skip
  109. if (name.Find("[0]") == String::NPOS)
  110. continue;
  111. name = name.Substring(0, index);
  112. }
  113. if (name[0] == 'c')
  114. {
  115. // Store the constant uniform mapping
  116. String paramName = name.Substring(1);
  117. ShaderParameter newParam;
  118. newParam.location_ = location;
  119. newParam.type_ = type;
  120. shaderParameters_[StringHash(paramName)] = newParam;
  121. }
  122. else if (name[0] == 's')
  123. {
  124. // Set the samplers here so that they do not have to be set later
  125. int unit = graphics_->GetTextureUnit(name.Substring(1));
  126. if (unit >= MAX_TEXTURE_UNITS)
  127. {
  128. // If texture unit name is not recognized, search for a digit in the name and use that as the unit index
  129. for (unsigned j = 1; j < name.Length(); ++j)
  130. {
  131. if (name[j] >= '0' && name[j] <= '9')
  132. {
  133. unit = name[j] - '0';
  134. break;
  135. }
  136. }
  137. }
  138. if (unit < MAX_TEXTURE_UNITS)
  139. {
  140. useTextureUnit_[unit] = true;
  141. glUniform1iv(location, 1, &unit);
  142. }
  143. }
  144. }
  145. // Query the vertex attribute bindings
  146. attributeLocations_[0] = glGetAttribLocation(object_, "iPos");
  147. attributeLocations_[1] = glGetAttribLocation(object_, "iNormal");
  148. attributeLocations_[2] = glGetAttribLocation(object_, "iColor");
  149. attributeLocations_[3] = glGetAttribLocation(object_, "iTexCoord");
  150. attributeLocations_[4] = glGetAttribLocation(object_, "iTexCoord2");
  151. attributeLocations_[5] = glGetAttribLocation(object_, "iCubeTexCoord");
  152. attributeLocations_[6] = glGetAttribLocation(object_, "iCubeTexCoord2");
  153. attributeLocations_[7] = glGetAttribLocation(object_, "iTangent");
  154. attributeLocations_[8] = glGetAttribLocation(object_, "iBlendWeights");
  155. attributeLocations_[9] = glGetAttribLocation(object_, "iBlendIndices");
  156. // Rehash the parameter map to ensure minimal load factor
  157. shaderParameters_.Rehash(NextPowerOfTwo(shaderParameters_.Size()));
  158. return true;
  159. }
  160. bool ShaderProgram::NeedParameterUpdate(StringHash param, const void* source, unsigned frame)
  161. {
  162. // If global parameter frame has changed, clear all remembered sources
  163. if (frame != lastParameterFrame_)
  164. {
  165. lastParameterFrame_ = frame;
  166. for (HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Begin(); i != shaderParameters_.End(); ++i)
  167. i->second_.lastSource_ = (const void*)M_MAX_UNSIGNED;
  168. }
  169. HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Find(param);
  170. if (i == shaderParameters_.End() || i->second_.lastSource_ == source)
  171. return false;
  172. i->second_.lastSource_ = source;
  173. return true;
  174. }
  175. void ShaderProgram::ClearParameterSource(StringHash param)
  176. {
  177. HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Find(param);
  178. if (i == shaderParameters_.End())
  179. return;
  180. i->second_.lastSource_ = (const void*)M_MAX_UNSIGNED;
  181. }
  182. ShaderVariation* ShaderProgram::GetVertexShader() const
  183. {
  184. return vertexShader_;
  185. }
  186. ShaderVariation* ShaderProgram::GetPixelShader() const
  187. {
  188. return pixelShader_;
  189. }
  190. bool ShaderProgram::HasParameter(StringHash param) const
  191. {
  192. return shaderParameters_.Find(param) != shaderParameters_.End();
  193. }
  194. const ShaderParameter* ShaderProgram::GetParameter(StringHash param) const
  195. {
  196. HashMap<StringHash, ShaderParameter>::ConstIterator i = shaderParameters_.Find(param);
  197. if (i != shaderParameters_.End())
  198. return &i->second_;
  199. else
  200. return 0;
  201. }