OGLShaderProgram.cpp 7.4 KB

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