2
0

OGLShaderProgram.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. 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. if ((vertexShader_) && (vertexShader_->GetGPUObject()))
  50. glDetachShader(object_, vertexShader_->GetGPUObject());
  51. if ((pixelShader_) && (pixelShader_->GetGPUObject()))
  52. glDetachShader(object_, pixelShader_->GetGPUObject());
  53. for (unsigned i = 0; i < MAX_TEXTURE_UNITS; ++i)
  54. useTextureUnit_[i] = false;
  55. uniformInfos_.Clear();
  56. glDeleteProgram(object_);
  57. object_ = 0;
  58. linked_ = false;
  59. }
  60. }
  61. bool ShaderProgram::Link()
  62. {
  63. Release();
  64. if ((!vertexShader_) || (!pixelShader_) || (!vertexShader_->GetGPUObject()) || (!pixelShader_->GetGPUObject()))
  65. return false;
  66. object_ = glCreateProgram();
  67. if (!object_)
  68. {
  69. linkerOutput_ = "Could not create shader program";
  70. return false;
  71. }
  72. glAttachShader(object_, vertexShader_->GetGPUObject());
  73. glAttachShader(object_, pixelShader_->GetGPUObject());
  74. glLinkProgram(object_);
  75. int linked, length;
  76. glGetProgramiv(object_, GL_LINK_STATUS, &linked);
  77. linked_ = linked != 0;
  78. if (!linked_)
  79. {
  80. glGetProgramiv(object_, GL_INFO_LOG_LENGTH, &length);
  81. linkerOutput_.Resize(length);
  82. glGetShaderInfoLog(object_, length, &length, &linkerOutput_[0]);
  83. }
  84. else
  85. linkerOutput_.Clear();
  86. if (!linked_)
  87. return false;
  88. const int MAX_PARAMETER_NAME_LENGTH = 256;
  89. char uniformName[MAX_PARAMETER_NAME_LENGTH];
  90. int uniformCount;
  91. glUseProgram(object_);
  92. glGetProgramiv(object_, GL_ACTIVE_UNIFORMS, &uniformCount);
  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. ShaderParameter param = graphics_->GetShaderParameter(name.Substring(1));
  117. if (param < MAX_SHADER_PARAMETERS)
  118. {
  119. UniformInfo newInfo;
  120. newInfo.location_ = location;
  121. newInfo.type_ = type;
  122. uniformInfos_[param] = newInfo;
  123. }
  124. }
  125. else if (name[0] == 's')
  126. {
  127. // Set the samplers here so that they do not have to be set later
  128. int unit = graphics_->GetTextureUnit(name.Substring(1));
  129. if (unit < MAX_TEXTURE_UNITS)
  130. {
  131. useTextureUnit_[unit] = true;
  132. glUniform1iv(location, 1, &unit);
  133. }
  134. }
  135. }
  136. // Query the vertex attribute bindings
  137. attributeLocations_[0] = glGetAttribLocation(object_, "iPosition");
  138. attributeLocations_[1] = glGetAttribLocation(object_, "iNormal");
  139. attributeLocations_[2] = glGetAttribLocation(object_, "iColor");
  140. attributeLocations_[3] = glGetAttribLocation(object_, "iTexCoord");
  141. attributeLocations_[4] = glGetAttribLocation(object_, "iTexCoord2");
  142. attributeLocations_[5] = glGetAttribLocation(object_, "iCubeTexCoord");
  143. attributeLocations_[6] = glGetAttribLocation(object_, "iCubeTexCoord2");
  144. attributeLocations_[7] = glGetAttribLocation(object_, "iTangent");
  145. attributeLocations_[8] = glGetAttribLocation(object_, "iBlendWeights");
  146. attributeLocations_[9] = glGetAttribLocation(object_, "iBlendIndices");
  147. return true;
  148. }
  149. ShaderVariation* ShaderProgram::GetVertexShader() const
  150. {
  151. return vertexShader_;
  152. }
  153. ShaderVariation* ShaderProgram::GetPixelShader() const
  154. {
  155. return pixelShader_;
  156. }
  157. bool ShaderProgram::HasParameter(ShaderParameter param) const
  158. {
  159. return uniformInfos_.Find(param) != uniformInfos_.End();
  160. }
  161. const UniformInfo* ShaderProgram::GetUniformInfo(ShaderParameter param) const
  162. {
  163. HashMap<ShaderParameter, UniformInfo>::ConstIterator i = uniformInfos_.Find(param);
  164. if (i != uniformInfos_.End())
  165. return &i->second_;
  166. else
  167. return 0;
  168. }