OGLShaderProgram.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "Precompiled.h"
  23. #include "Graphics.h"
  24. #include "GraphicsImpl.h"
  25. #include "ShaderProgram.h"
  26. #include "ShaderVariation.h"
  27. #include "DebugNew.h"
  28. namespace Urho3D
  29. {
  30. ShaderProgram::ShaderProgram(Graphics* graphics, ShaderVariation* vertexShader, ShaderVariation* pixelShader) :
  31. GPUObject(graphics),
  32. vertexShader_(vertexShader),
  33. pixelShader_(pixelShader)
  34. {
  35. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  36. useTextureUnit_[i] = false;
  37. }
  38. ShaderProgram::~ShaderProgram()
  39. {
  40. Release();
  41. }
  42. void ShaderProgram::OnDeviceLost()
  43. {
  44. GPUObject::OnDeviceLost();
  45. if (graphics_ && graphics_->GetShaderProgram() == this)
  46. graphics_->SetShaders(0, 0);
  47. linkerOutput_.Clear();
  48. }
  49. void ShaderProgram::Release()
  50. {
  51. if (object_)
  52. {
  53. if (!graphics_)
  54. return;
  55. if (!graphics_->IsDeviceLost())
  56. {
  57. if (graphics_->GetShaderProgram() == this)
  58. graphics_->SetShaders(0, 0);
  59. glDeleteProgram(object_);
  60. }
  61. object_ = 0;
  62. linkerOutput_.Clear();
  63. shaderParameters_.Clear();
  64. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  65. useTextureUnit_[i] = false;
  66. }
  67. }
  68. bool ShaderProgram::Link()
  69. {
  70. Release();
  71. if (!vertexShader_ || !pixelShader_ || !vertexShader_->GetGPUObject() || !pixelShader_->GetGPUObject())
  72. return false;
  73. object_ = glCreateProgram();
  74. if (!object_)
  75. {
  76. linkerOutput_ = "Could not create shader program";
  77. return false;
  78. }
  79. // Bind vertex attribute locations to ensure they are the same in all shaders
  80. // Note: this is not the same order as in VertexBuffer, instead a remapping is used to ensure everything except cube texture
  81. // coordinates fit to the first 8 for better GLES2 device compatibility
  82. glBindAttribLocation(object_, 0, "iPos");
  83. glBindAttribLocation(object_, 1, "iNormal");
  84. glBindAttribLocation(object_, 2, "iColor");
  85. glBindAttribLocation(object_, 3, "iTexCoord");
  86. glBindAttribLocation(object_, 4, "iTexCoord2");
  87. glBindAttribLocation(object_, 5, "iTangent");
  88. glBindAttribLocation(object_, 6, "iBlendWeights");
  89. glBindAttribLocation(object_, 7, "iBlendIndices");
  90. glBindAttribLocation(object_, 8, "iCubeTexCoord");
  91. glBindAttribLocation(object_, 9, "iCubeTexCoord2");
  92. #ifndef GL_ES_VERSION_2_0
  93. glBindAttribLocation(object_, 10, "iInstanceMatrix1");
  94. glBindAttribLocation(object_, 11, "iInstanceMatrix2");
  95. glBindAttribLocation(object_, 12, "iInstanceMatrix3");
  96. #endif
  97. glAttachShader(object_, vertexShader_->GetGPUObject());
  98. glAttachShader(object_, pixelShader_->GetGPUObject());
  99. glLinkProgram(object_);
  100. int linked, length;
  101. glGetProgramiv(object_, GL_LINK_STATUS, &linked);
  102. if (!linked)
  103. {
  104. glGetProgramiv(object_, GL_INFO_LOG_LENGTH, &length);
  105. linkerOutput_.Resize(length);
  106. int outLength;
  107. glGetProgramInfoLog(object_, length, &outLength, &linkerOutput_[0]);
  108. glDeleteProgram(object_);
  109. object_ = 0;
  110. }
  111. else
  112. linkerOutput_.Clear();
  113. if (!object_)
  114. return false;
  115. const int MAX_PARAMETER_NAME_LENGTH = 256;
  116. char uniformName[MAX_PARAMETER_NAME_LENGTH];
  117. int uniformCount;
  118. glUseProgram(object_);
  119. glGetProgramiv(object_, GL_ACTIVE_UNIFORMS, &uniformCount);
  120. // Check for shader parameters and texture units
  121. for (int i = 0; i < uniformCount; ++i)
  122. {
  123. unsigned type;
  124. int count;
  125. glGetActiveUniform(object_, i, MAX_PARAMETER_NAME_LENGTH, 0, &count, &type, uniformName);
  126. int location = glGetUniformLocation(object_, uniformName);
  127. // Skip inbuilt or disabled uniforms
  128. if (location < 0)
  129. continue;
  130. // Check for array index included in the name and strip it
  131. String name(uniformName);
  132. unsigned index = name.Find('[');
  133. if (index != String::NPOS)
  134. {
  135. // If not the first index, skip
  136. if (name.Find("[0]", index) == String::NPOS)
  137. continue;
  138. name = name.Substring(0, index);
  139. }
  140. if (name[0] == 'c')
  141. {
  142. // Store the constant uniform mapping
  143. String paramName = name.Substring(1);
  144. ShaderParameter newParam;
  145. newParam.location_ = location;
  146. newParam.type_ = type;
  147. shaderParameters_[StringHash(paramName)] = newParam;
  148. }
  149. else if (name[0] == 's')
  150. {
  151. // Set the samplers here so that they do not have to be set later
  152. int unit = graphics_->GetTextureUnit(name.Substring(1));
  153. if (unit >= MAX_TEXTURE_UNITS)
  154. {
  155. // If texture unit name is not recognized, search for a digit in the name and use that as the unit index
  156. for (unsigned j = 1; j < name.Length(); ++j)
  157. {
  158. if (name[j] >= '0' && name[j] <= '9')
  159. {
  160. unit = name[j] - '0';
  161. break;
  162. }
  163. }
  164. }
  165. if (unit < MAX_TEXTURE_UNITS)
  166. {
  167. useTextureUnit_[unit] = true;
  168. glUniform1iv(location, 1, &unit);
  169. }
  170. }
  171. }
  172. // Rehash the parameter map to ensure minimal load factor
  173. shaderParameters_.Rehash(NextPowerOfTwo(shaderParameters_.Size()));
  174. return true;
  175. }
  176. ShaderVariation* ShaderProgram::GetVertexShader() const
  177. {
  178. return vertexShader_;
  179. }
  180. ShaderVariation* ShaderProgram::GetPixelShader() const
  181. {
  182. return pixelShader_;
  183. }
  184. bool ShaderProgram::HasParameter(StringHash param) const
  185. {
  186. return shaderParameters_.Find(param) != shaderParameters_.End();
  187. }
  188. const ShaderParameter* ShaderProgram::GetParameter(StringHash param) const
  189. {
  190. HashMap<StringHash, ShaderParameter>::ConstIterator i = shaderParameters_.Find(param);
  191. if (i != shaderParameters_.End())
  192. return &i->second_;
  193. else
  194. return 0;
  195. }
  196. }