OGLShaderVariation.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../../Graphics/Graphics.h"
  23. #include "../../Graphics/GraphicsImpl.h"
  24. #include "../../IO/Log.h"
  25. #include "../../Graphics/Shader.h"
  26. #include "../../Graphics/ShaderProgram.h"
  27. #include "../../Graphics/ShaderVariation.h"
  28. #include "../../DebugNew.h"
  29. namespace Urho3D
  30. {
  31. ShaderVariation::ShaderVariation(Shader* owner, ShaderType type) :
  32. GPUObject(owner->GetSubsystem<Graphics>()),
  33. owner_(owner),
  34. type_(type)
  35. {
  36. }
  37. ShaderVariation::~ShaderVariation()
  38. {
  39. Release();
  40. }
  41. void ShaderVariation::OnDeviceLost()
  42. {
  43. GPUObject::OnDeviceLost();
  44. compilerOutput_.Clear();
  45. }
  46. void ShaderVariation::Release()
  47. {
  48. if (object_)
  49. {
  50. if (!graphics_)
  51. return;
  52. if (!graphics_->IsDeviceLost())
  53. {
  54. if (type_ == VS)
  55. {
  56. if (graphics_->GetVertexShader() == this)
  57. graphics_->SetShaders(0, 0);
  58. }
  59. else
  60. {
  61. if (graphics_->GetPixelShader() == this)
  62. graphics_->SetShaders(0, 0);
  63. }
  64. glDeleteShader(object_);
  65. }
  66. object_ = 0;
  67. graphics_->CleanupShaderPrograms(this);
  68. }
  69. compilerOutput_.Clear();
  70. }
  71. bool ShaderVariation::Create()
  72. {
  73. Release();
  74. if (!owner_)
  75. {
  76. compilerOutput_ = "Owner shader has expired";
  77. return false;
  78. }
  79. object_ = glCreateShader(type_ == VS ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER);
  80. if (!object_)
  81. {
  82. compilerOutput_ = "Could not create shader object";
  83. return false;
  84. }
  85. const String& originalShaderCode = owner_->GetSourceCode(type_);
  86. String shaderCode;
  87. // Check if the shader code contains a version define
  88. unsigned verStart = originalShaderCode.Find('#');
  89. unsigned verEnd = 0;
  90. if (verStart != String::NPOS)
  91. {
  92. if (originalShaderCode.Substring(verStart + 1, 7) == "version")
  93. {
  94. verEnd = verStart + 9;
  95. while (verEnd < originalShaderCode.Length())
  96. {
  97. if (IsDigit(originalShaderCode[verEnd]))
  98. ++verEnd;
  99. else
  100. break;
  101. }
  102. // If version define found, insert it first
  103. String versionDefine = originalShaderCode.Substring(verStart, verEnd - verStart);
  104. shaderCode += versionDefine + "\n";
  105. }
  106. }
  107. // Force GLSL version 150 if no version define and GL3 is being used
  108. if (!verEnd && Graphics::GetGL3Support())
  109. shaderCode += "#version 150\n";
  110. // Distinguish between VS and PS compile in case the shader code wants to include/omit different things
  111. shaderCode += type_ == VS ? "#define COMPILEVS\n" : "#define COMPILEPS\n";
  112. // Add define for the maximum number of supported bones
  113. shaderCode += "#define MAXBONES " + String(Graphics::GetMaxBones()) + "\n";
  114. // Prepend the defines to the shader code
  115. Vector<String> defineVec = defines_.Split(' ');
  116. for (unsigned i = 0; i < defineVec.Size(); ++i)
  117. {
  118. // Add extra space for the checking code below
  119. String defineString = "#define " + defineVec[i].Replaced('=', ' ') + " \n";
  120. shaderCode += defineString;
  121. // In debug mode, check that all defines are referenced by the shader code
  122. #ifdef _DEBUG
  123. String defineCheck = defineString.Substring(8, defineString.Find(' ', 8) - 8);
  124. if (originalShaderCode.Find(defineCheck) == String::NPOS)
  125. LOGWARNING("Shader " + GetFullName() + " does not use the define " + defineCheck);
  126. #endif
  127. }
  128. #ifdef RPI
  129. if (type_ == VS)
  130. shaderCode += "#define RPI\n";
  131. #endif
  132. #ifdef EMSCRIPTEN
  133. shaderCode += "#define WEBGL\n";
  134. #endif
  135. if (Graphics::GetGL3Support())
  136. shaderCode += "#define GL3\n";
  137. // When version define found, do not insert it a second time
  138. if (verEnd > 0)
  139. shaderCode += (originalShaderCode.CString() + verEnd);
  140. else
  141. shaderCode += originalShaderCode;
  142. const char* shaderCStr = shaderCode.CString();
  143. glShaderSource(object_, 1, &shaderCStr, 0);
  144. glCompileShader(object_);
  145. int compiled, length;
  146. glGetShaderiv(object_, GL_COMPILE_STATUS, &compiled);
  147. if (!compiled)
  148. {
  149. glGetShaderiv(object_, GL_INFO_LOG_LENGTH, &length);
  150. compilerOutput_.Resize(length);
  151. int outLength;
  152. glGetShaderInfoLog(object_, length, &outLength, &compilerOutput_[0]);
  153. glDeleteShader(object_);
  154. object_ = 0;
  155. }
  156. else
  157. compilerOutput_.Clear();
  158. return object_ != 0;
  159. }
  160. void ShaderVariation::SetName(const String& name)
  161. {
  162. name_ = name;
  163. }
  164. void ShaderVariation::SetDefines(const String& defines)
  165. {
  166. defines_ = defines;
  167. }
  168. Shader* ShaderVariation::GetOwner() const
  169. {
  170. return owner_;
  171. }
  172. }