OGLShaderProgram.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. useTextureUnit_[unit] = true;
  129. glUniform1iv(location, 1, &unit);
  130. }
  131. }
  132. }
  133. // Query the vertex attribute bindings
  134. attributeLocations_[0] = glGetAttribLocation(object_, "iPosition");
  135. attributeLocations_[1] = glGetAttribLocation(object_, "iNormal");
  136. attributeLocations_[2] = glGetAttribLocation(object_, "iColor");
  137. attributeLocations_[3] = glGetAttribLocation(object_, "iTexCoord");
  138. attributeLocations_[4] = glGetAttribLocation(object_, "iTexCoord2");
  139. attributeLocations_[5] = glGetAttribLocation(object_, "iCubeTexCoord");
  140. attributeLocations_[6] = glGetAttribLocation(object_, "iCubeTexCoord2");
  141. attributeLocations_[7] = glGetAttribLocation(object_, "iTangent");
  142. attributeLocations_[8] = glGetAttribLocation(object_, "iBlendWeights");
  143. attributeLocations_[9] = glGetAttribLocation(object_, "iBlendIndices");
  144. // Rehash the parameter map to ensure minimal load factor
  145. shaderParameters_.Rehash(NextPowerOfTwo(shaderParameters_.Size()));
  146. return true;
  147. }
  148. bool ShaderProgram::NeedParameterUpdate(StringHash param, const void* source, unsigned frame)
  149. {
  150. // If global parameter frame has changed, clear all remembered sources
  151. if (frame != lastParameterFrame_)
  152. {
  153. lastParameterFrame_ = frame;
  154. for (HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Begin(); i != shaderParameters_.End(); ++i)
  155. i->second_.lastSource_ = (const void*)M_MAX_UNSIGNED;
  156. }
  157. HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Find(param);
  158. if (i == shaderParameters_.End() || i->second_.lastSource_ == source)
  159. return false;
  160. i->second_.lastSource_ = source;
  161. return true;
  162. }
  163. void ShaderProgram::ClearParameterSource(StringHash param)
  164. {
  165. HashMap<StringHash, ShaderParameter>::Iterator i = shaderParameters_.Find(param);
  166. if (i == shaderParameters_.End())
  167. return;
  168. i->second_.lastSource_ = (const void*)M_MAX_UNSIGNED;
  169. }
  170. ShaderVariation* ShaderProgram::GetVertexShader() const
  171. {
  172. return vertexShader_;
  173. }
  174. ShaderVariation* ShaderProgram::GetPixelShader() const
  175. {
  176. return pixelShader_;
  177. }
  178. bool ShaderProgram::HasParameter(StringHash param) const
  179. {
  180. return shaderParameters_.Find(param) != shaderParameters_.End();
  181. }
  182. const ShaderParameter* ShaderProgram::GetParameter(StringHash param) const
  183. {
  184. HashMap<StringHash, ShaderParameter>::ConstIterator i = shaderParameters_.Find(param);
  185. if (i != shaderParameters_.End())
  186. return &i->second_;
  187. else
  188. return 0;
  189. }