OGLShaderProgram.cpp 7.1 KB

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