OGLShaderProgram.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  58. useTextureUnit_[i] = false;
  59. shaderParameters_.Clear();
  60. if (graphics_->GetShaderProgram() == this)
  61. graphics_->SetShaders(0, 0);
  62. glDeleteProgram(object_);
  63. object_ = 0;
  64. linked_ = false;
  65. linkerOutput_.Clear();
  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. linked_ = linked != 0;
  103. if (!linked_)
  104. {
  105. glGetProgramiv(object_, GL_INFO_LOG_LENGTH, &length);
  106. linkerOutput_.Resize(length);
  107. int outLength;
  108. glGetProgramInfoLog(object_, length, &outLength, &linkerOutput_[0]);
  109. }
  110. else
  111. linkerOutput_.Clear();
  112. if (!linked_)
  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. }