OGLShaderProgram.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. glAttachShader(object_, vertexShader_->GetGPUObject());
  93. glAttachShader(object_, pixelShader_->GetGPUObject());
  94. glLinkProgram(object_);
  95. int linked, length;
  96. glGetProgramiv(object_, GL_LINK_STATUS, &linked);
  97. linked_ = linked != 0;
  98. if (!linked_)
  99. {
  100. glGetProgramiv(object_, GL_INFO_LOG_LENGTH, &length);
  101. linkerOutput_.Resize(length);
  102. int outLength;
  103. glGetProgramInfoLog(object_, length, &outLength, &linkerOutput_[0]);
  104. }
  105. else
  106. linkerOutput_.Clear();
  107. if (!linked_)
  108. return false;
  109. const int MAX_PARAMETER_NAME_LENGTH = 256;
  110. char uniformName[MAX_PARAMETER_NAME_LENGTH];
  111. int uniformCount;
  112. glUseProgram(object_);
  113. glGetProgramiv(object_, GL_ACTIVE_UNIFORMS, &uniformCount);
  114. // Check for shader parameters and texture units
  115. for (int i = 0; i < uniformCount; ++i)
  116. {
  117. unsigned type;
  118. int count;
  119. glGetActiveUniform(object_, i, MAX_PARAMETER_NAME_LENGTH, 0, &count, &type, uniformName);
  120. int location = glGetUniformLocation(object_, uniformName);
  121. // Skip inbuilt or disabled uniforms
  122. if (location < 0)
  123. continue;
  124. // Check for array index included in the name and strip it
  125. String name(uniformName);
  126. unsigned index = name.Find('[');
  127. if (index != String::NPOS)
  128. {
  129. // If not the first index, skip
  130. if (name.Find("[0]", index) == String::NPOS)
  131. continue;
  132. name = name.Substring(0, index);
  133. }
  134. if (name[0] == 'c')
  135. {
  136. // Store the constant uniform mapping
  137. String paramName = name.Substring(1);
  138. ShaderParameter newParam;
  139. newParam.location_ = location;
  140. newParam.type_ = type;
  141. shaderParameters_[StringHash(paramName)] = newParam;
  142. }
  143. else if (name[0] == 's')
  144. {
  145. // Set the samplers here so that they do not have to be set later
  146. int unit = graphics_->GetTextureUnit(name.Substring(1));
  147. if (unit >= MAX_TEXTURE_UNITS)
  148. {
  149. // If texture unit name is not recognized, search for a digit in the name and use that as the unit index
  150. for (unsigned j = 1; j < name.Length(); ++j)
  151. {
  152. if (name[j] >= '0' && name[j] <= '9')
  153. {
  154. unit = name[j] - '0';
  155. break;
  156. }
  157. }
  158. }
  159. if (unit < MAX_TEXTURE_UNITS)
  160. {
  161. useTextureUnit_[unit] = true;
  162. glUniform1iv(location, 1, &unit);
  163. }
  164. }
  165. }
  166. // Rehash the parameter map to ensure minimal load factor
  167. shaderParameters_.Rehash(NextPowerOfTwo(shaderParameters_.Size()));
  168. return true;
  169. }
  170. ShaderVariation* ShaderProgram::GetVertexShader() const
  171. {
  172. return vertexShader_;
  173. }
  174. ShaderVariation* ShaderProgram::GetPixelShader() const
  175. {
  176. return pixelShader_;
  177. }
  178. bool ShaderProgram::HasParameter(StringHash param) const
  179. {
  180. return shaderParameters_.Find(param) != shaderParameters_.End();
  181. }
  182. const ShaderParameter* ShaderProgram::GetParameter(StringHash param) const
  183. {
  184. HashMap<StringHash, ShaderParameter>::ConstIterator i = shaderParameters_.Find(param);
  185. if (i != shaderParameters_.End())
  186. return &i->second_;
  187. else
  188. return 0;
  189. }
  190. }