OGLShaderProgram.cpp 7.1 KB

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