OGLShaderProgram.cpp 6.8 KB

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