OGLShaderProgram.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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 <GLee.h>
  29. #include "DebugNew.h"
  30. ShaderProgram::ShaderProgram(Graphics* graphics, ShaderVariation* vertexShader, ShaderVariation* pixelShader) :
  31. GPUObject(graphics),
  32. vertexShader_(vertexShader),
  33. pixelShader_(pixelShader),
  34. lastParameterFrame_(0),
  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::Release()
  45. {
  46. if (object_)
  47. {
  48. if (!graphics_)
  49. return;
  50. if ((vertexShader_) && (vertexShader_->GetGPUObject()))
  51. glDetachShader(object_, vertexShader_->GetGPUObject());
  52. if ((pixelShader_) && (pixelShader_->GetGPUObject()))
  53. glDetachShader(object_, pixelShader_->GetGPUObject());
  54. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  55. useTextureUnit_[i] = false;
  56. uniformInfos_.Clear();
  57. glDeleteProgram(object_);
  58. object_ = 0;
  59. linked_ = false;
  60. linkerOutput_.Clear();
  61. }
  62. }
  63. bool ShaderProgram::Link()
  64. {
  65. Release();
  66. if ((!vertexShader_) || (!pixelShader_) || (!vertexShader_->GetGPUObject()) || (!pixelShader_->GetGPUObject()))
  67. return false;
  68. object_ = glCreateProgram();
  69. if (!object_)
  70. {
  71. linkerOutput_ = "Could not create shader program";
  72. return false;
  73. }
  74. glAttachShader(object_, vertexShader_->GetGPUObject());
  75. glAttachShader(object_, pixelShader_->GetGPUObject());
  76. glLinkProgram(object_);
  77. int linked, length;
  78. glGetProgramiv(object_, GL_LINK_STATUS, &linked);
  79. linked_ = linked != 0;
  80. if (!linked_)
  81. {
  82. glGetProgramiv(object_, GL_INFO_LOG_LENGTH, &length);
  83. linkerOutput_.Resize(length);
  84. glGetShaderInfoLog(object_, length, &length, &linkerOutput_[0]);
  85. }
  86. else
  87. linkerOutput_.Clear();
  88. if (!linked_)
  89. return false;
  90. const int MAX_PARAMETER_NAME_LENGTH = 256;
  91. char uniformName[MAX_PARAMETER_NAME_LENGTH];
  92. int uniformCount;
  93. glUseProgram(object_);
  94. glGetProgramiv(object_, GL_ACTIVE_UNIFORMS, &uniformCount);
  95. for (int i = 0; i < uniformCount; ++i)
  96. {
  97. int location;
  98. unsigned type;
  99. int count;
  100. glGetActiveUniform(object_, i, MAX_PARAMETER_NAME_LENGTH, 0, &count, &type, uniformName);
  101. location = glGetUniformLocation(object_, uniformName);
  102. // Skip inbuilt or disabled uniforms
  103. if (location < 0)
  104. continue;
  105. // Check for array index included in the name and strip it
  106. String name(uniformName);
  107. unsigned index = name.Find('[');
  108. if (index != String::NPOS)
  109. {
  110. // If not the first index, skip
  111. if (name.Find("[0]") == String::NPOS)
  112. continue;
  113. name = name.Substring(0, index);
  114. }
  115. if (name[0] == 'c')
  116. {
  117. // Store the constant uniform mapping
  118. ShaderParameter param = graphics_->GetShaderParameter(name.Substring(1));
  119. if (param < MAX_SHADER_PARAMETERS)
  120. {
  121. UniformInfo newInfo;
  122. newInfo.location_ = location;
  123. newInfo.type_ = type;
  124. uniformInfos_[param] = newInfo;
  125. }
  126. }
  127. else if (name[0] == 's')
  128. {
  129. // Set the samplers here so that they do not have to be set later
  130. int unit = graphics_->GetTextureUnit(name.Substring(1));
  131. if (unit < MAX_TEXTURE_UNITS)
  132. {
  133. useTextureUnit_[unit] = true;
  134. glUniform1iv(location, 1, &unit);
  135. }
  136. }
  137. }
  138. // Query the vertex attribute bindings
  139. attributeLocations_[0] = glGetAttribLocation(object_, "iPosition");
  140. attributeLocations_[1] = glGetAttribLocation(object_, "iNormal");
  141. attributeLocations_[2] = glGetAttribLocation(object_, "iColor");
  142. attributeLocations_[3] = glGetAttribLocation(object_, "iTexCoord");
  143. attributeLocations_[4] = glGetAttribLocation(object_, "iTexCoord2");
  144. attributeLocations_[5] = glGetAttribLocation(object_, "iCubeTexCoord");
  145. attributeLocations_[6] = glGetAttribLocation(object_, "iCubeTexCoord2");
  146. attributeLocations_[7] = glGetAttribLocation(object_, "iTangent");
  147. attributeLocations_[8] = glGetAttribLocation(object_, "iBlendWeights");
  148. attributeLocations_[9] = glGetAttribLocation(object_, "iBlendIndices");
  149. return true;
  150. }
  151. bool ShaderProgram::NeedParameterUpdate(ShaderParameter param, const void* source, unsigned int frame)
  152. {
  153. // If global parameter frame has changed, clear all remembered sources
  154. if (frame != lastParameterFrame_)
  155. {
  156. lastParameterFrame_ = frame;
  157. lastParameterSources_.Clear();
  158. }
  159. if (uniformInfos_.Find(param) == uniformInfos_.End())
  160. return false;
  161. HashMap<ShaderParameter, const void*>::Iterator i = lastParameterSources_.Find(param);
  162. if (i != lastParameterSources_.End())
  163. {
  164. if (i->second_ == source)
  165. return false;
  166. i->second_ = source;
  167. return true;
  168. }
  169. lastParameterSources_[param] = source;
  170. return true;
  171. }
  172. void ShaderProgram::ClearParameterSource(ShaderParameter param)
  173. {
  174. lastParameterSources_.Erase(param);
  175. }
  176. ShaderVariation* ShaderProgram::GetVertexShader() const
  177. {
  178. return vertexShader_;
  179. }
  180. ShaderVariation* ShaderProgram::GetPixelShader() const
  181. {
  182. return pixelShader_;
  183. }
  184. bool ShaderProgram::HasParameter(ShaderParameter param) const
  185. {
  186. return uniformInfos_.Find(param) != uniformInfos_.End();
  187. }
  188. const UniformInfo* ShaderProgram::GetUniformInfo(ShaderParameter param) const
  189. {
  190. HashMap<ShaderParameter, UniformInfo>::ConstIterator i = uniformInfos_.Find(param);
  191. if (i != uniformInfos_.End())
  192. return &i->second_;
  193. else
  194. return 0;
  195. }