OGLShaderProgram.cpp 6.9 KB

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