OGLShaderProgram.cpp 6.9 KB

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