OGLShaderProgram.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../../Graphics/Graphics.h"
  23. #include "../../Graphics/GraphicsImpl.h"
  24. #include "../../Graphics/ShaderProgram.h"
  25. #include "../../Graphics/ShaderVariation.h"
  26. #include "../../DebugNew.h"
  27. namespace Urho3D
  28. {
  29. ShaderProgram::ShaderProgram(Graphics* graphics, ShaderVariation* vertexShader, ShaderVariation* pixelShader) :
  30. GPUObject(graphics),
  31. vertexShader_(vertexShader),
  32. pixelShader_(pixelShader)
  33. {
  34. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  35. useTextureUnit_[i] = false;
  36. }
  37. ShaderProgram::~ShaderProgram()
  38. {
  39. Release();
  40. }
  41. void ShaderProgram::OnDeviceLost()
  42. {
  43. GPUObject::OnDeviceLost();
  44. if (graphics_ && graphics_->GetShaderProgram() == this)
  45. graphics_->SetShaders(0, 0);
  46. linkerOutput_.Clear();
  47. }
  48. void ShaderProgram::Release()
  49. {
  50. if (object_)
  51. {
  52. if (!graphics_)
  53. return;
  54. if (!graphics_->IsDeviceLost())
  55. {
  56. if (graphics_->GetShaderProgram() == this)
  57. graphics_->SetShaders(0, 0);
  58. glDeleteProgram(object_);
  59. }
  60. object_ = 0;
  61. linkerOutput_.Clear();
  62. shaderParameters_.Clear();
  63. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  64. useTextureUnit_[i] = false;
  65. }
  66. }
  67. bool ShaderProgram::Link()
  68. {
  69. Release();
  70. if (!vertexShader_ || !pixelShader_ || !vertexShader_->GetGPUObject() || !pixelShader_->GetGPUObject())
  71. return false;
  72. object_ = glCreateProgram();
  73. if (!object_)
  74. {
  75. linkerOutput_ = "Could not create shader program";
  76. return false;
  77. }
  78. // Bind vertex attribute locations to ensure they are the same in all shaders
  79. // Note: this is not the same order as in VertexBuffer, instead a remapping is used to ensure everything except cube texture
  80. // coordinates fit to the first 8 for better GLES2 device compatibility
  81. glBindAttribLocation(object_, 0, "iPos");
  82. glBindAttribLocation(object_, 1, "iNormal");
  83. glBindAttribLocation(object_, 2, "iColor");
  84. glBindAttribLocation(object_, 3, "iTexCoord");
  85. glBindAttribLocation(object_, 4, "iTexCoord2");
  86. glBindAttribLocation(object_, 5, "iTangent");
  87. glBindAttribLocation(object_, 6, "iBlendWeights");
  88. glBindAttribLocation(object_, 7, "iBlendIndices");
  89. glBindAttribLocation(object_, 8, "iCubeTexCoord");
  90. glBindAttribLocation(object_, 9, "iCubeTexCoord2");
  91. #ifndef GL_ES_VERSION_2_0
  92. glBindAttribLocation(object_, 10, "iInstanceMatrix1");
  93. glBindAttribLocation(object_, 11, "iInstanceMatrix2");
  94. glBindAttribLocation(object_, 12, "iInstanceMatrix3");
  95. #endif
  96. glAttachShader(object_, vertexShader_->GetGPUObject());
  97. glAttachShader(object_, pixelShader_->GetGPUObject());
  98. glLinkProgram(object_);
  99. int linked, length;
  100. glGetProgramiv(object_, GL_LINK_STATUS, &linked);
  101. if (!linked)
  102. {
  103. glGetProgramiv(object_, GL_INFO_LOG_LENGTH, &length);
  104. linkerOutput_.Resize(length);
  105. int outLength;
  106. glGetProgramInfoLog(object_, length, &outLength, &linkerOutput_[0]);
  107. glDeleteProgram(object_);
  108. object_ = 0;
  109. }
  110. else
  111. linkerOutput_.Clear();
  112. if (!object_)
  113. return false;
  114. const int MAX_PARAMETER_NAME_LENGTH = 256;
  115. char uniformName[MAX_PARAMETER_NAME_LENGTH];
  116. int uniformCount;
  117. glUseProgram(object_);
  118. glGetProgramiv(object_, GL_ACTIVE_UNIFORMS, &uniformCount);
  119. // Check for shader parameters and texture units
  120. for (int i = 0; i < uniformCount; ++i)
  121. {
  122. unsigned type;
  123. int count;
  124. glGetActiveUniform(object_, i, MAX_PARAMETER_NAME_LENGTH, 0, &count, &type, uniformName);
  125. int location = glGetUniformLocation(object_, uniformName);
  126. // Skip inbuilt or disabled uniforms
  127. if (location < 0)
  128. continue;
  129. // Check for array index included in the name and strip it
  130. String name(uniformName);
  131. unsigned index = name.Find('[');
  132. if (index != String::NPOS)
  133. {
  134. // If not the first index, skip
  135. if (name.Find("[0]", index) == String::NPOS)
  136. continue;
  137. name = name.Substring(0, index);
  138. }
  139. if (name[0] == 'c')
  140. {
  141. // Store the constant uniform mapping
  142. String paramName = name.Substring(1);
  143. ShaderParameter newParam;
  144. newParam.location_ = location;
  145. newParam.type_ = type;
  146. shaderParameters_[StringHash(paramName)] = newParam;
  147. }
  148. else if (name[0] == 's')
  149. {
  150. // Set the samplers here so that they do not have to be set later
  151. int unit = graphics_->GetTextureUnit(name.Substring(1));
  152. if (unit >= MAX_TEXTURE_UNITS)
  153. {
  154. // If texture unit name is not recognized, search for a digit in the name and use that as the unit index
  155. for (unsigned j = 1; j < name.Length(); ++j)
  156. {
  157. if (name[j] >= '0' && name[j] <= '9')
  158. {
  159. unit = name[j] - '0';
  160. break;
  161. }
  162. }
  163. }
  164. if (unit < MAX_TEXTURE_UNITS)
  165. {
  166. useTextureUnit_[unit] = true;
  167. glUniform1iv(location, 1, &unit);
  168. }
  169. }
  170. }
  171. // Rehash the parameter map to ensure minimal load factor
  172. shaderParameters_.Rehash(NextPowerOfTwo(shaderParameters_.Size()));
  173. return true;
  174. }
  175. ShaderVariation* ShaderProgram::GetVertexShader() const
  176. {
  177. return vertexShader_;
  178. }
  179. ShaderVariation* ShaderProgram::GetPixelShader() const
  180. {
  181. return pixelShader_;
  182. }
  183. bool ShaderProgram::HasParameter(StringHash param) const
  184. {
  185. return shaderParameters_.Find(param) != shaderParameters_.End();
  186. }
  187. const ShaderParameter* ShaderProgram::GetParameter(StringHash param) const
  188. {
  189. HashMap<StringHash, ShaderParameter>::ConstIterator i = shaderParameters_.Find(param);
  190. if (i != shaderParameters_.End())
  191. return &i->second_;
  192. else
  193. return 0;
  194. }
  195. }