OGLShaderVariation.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 "../../Precompiled.h"
  23. #include "../../Graphics/Graphics.h"
  24. #include "../../Graphics/GraphicsImpl.h"
  25. #include "../../Graphics/Shader.h"
  26. #include "../../Graphics/ShaderProgram.h"
  27. #include "../../Graphics/ShaderVariation.h"
  28. #include "../../IO/Log.h"
  29. #include "../../DebugNew.h"
  30. namespace Atomic
  31. {
  32. ShaderVariation::ShaderVariation(Shader* owner, ShaderType type) :
  33. GPUObject(owner->GetSubsystem<Graphics>()),
  34. owner_(owner),
  35. type_(type)
  36. {
  37. }
  38. ShaderVariation::~ShaderVariation()
  39. {
  40. Release();
  41. }
  42. void ShaderVariation::OnDeviceLost()
  43. {
  44. GPUObject::OnDeviceLost();
  45. compilerOutput_.Clear();
  46. }
  47. void ShaderVariation::Release()
  48. {
  49. if (object_)
  50. {
  51. if (!graphics_)
  52. return;
  53. if (!graphics_->IsDeviceLost())
  54. {
  55. if (type_ == VS)
  56. {
  57. if (graphics_->GetVertexShader() == this)
  58. graphics_->SetShaders(0, 0);
  59. }
  60. else
  61. {
  62. if (graphics_->GetPixelShader() == this)
  63. graphics_->SetShaders(0, 0);
  64. }
  65. glDeleteShader(object_);
  66. }
  67. object_ = 0;
  68. graphics_->CleanupShaderPrograms(this);
  69. }
  70. compilerOutput_.Clear();
  71. }
  72. bool ShaderVariation::Create()
  73. {
  74. Release();
  75. if (!owner_)
  76. {
  77. compilerOutput_ = "Owner shader has expired";
  78. return false;
  79. }
  80. object_ = glCreateShader(type_ == VS ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER);
  81. if (!object_)
  82. {
  83. compilerOutput_ = "Could not create shader object";
  84. return false;
  85. }
  86. const String& originalShaderCode = owner_->GetSourceCode(type_);
  87. String shaderCode;
  88. // Check if the shader code contains a version define
  89. unsigned verStart = originalShaderCode.Find('#');
  90. unsigned verEnd = 0;
  91. if (verStart != String::NPOS)
  92. {
  93. if (originalShaderCode.Substring(verStart + 1, 7) == "version")
  94. {
  95. verEnd = verStart + 9;
  96. while (verEnd < originalShaderCode.Length())
  97. {
  98. if (IsDigit((unsigned)originalShaderCode[verEnd]))
  99. ++verEnd;
  100. else
  101. break;
  102. }
  103. // If version define found, insert it first
  104. String versionDefine = originalShaderCode.Substring(verStart, verEnd - verStart);
  105. shaderCode += versionDefine + "\n";
  106. }
  107. }
  108. // Force GLSL version 150 if no version define and GL3 is being used
  109. if (!verEnd && Graphics::GetGL3Support())
  110. shaderCode += "#version 150\n";
  111. // Distinguish between VS and PS compile in case the shader code wants to include/omit different things
  112. shaderCode += type_ == VS ? "#define COMPILEVS\n" : "#define COMPILEPS\n";
  113. // Add define for the maximum number of supported bones
  114. shaderCode += "#define MAXBONES " + String(Graphics::GetMaxBones()) + "\n";
  115. // Prepend the defines to the shader code
  116. Vector<String> defineVec = defines_.Split(' ');
  117. for (unsigned i = 0; i < defineVec.Size(); ++i)
  118. {
  119. // Add extra space for the checking code below
  120. String defineString = "#define " + defineVec[i].Replaced('=', ' ') + " \n";
  121. shaderCode += defineString;
  122. // In debug mode, check that all defines are referenced by the shader code
  123. #ifdef _DEBUG
  124. String defineCheck = defineString.Substring(8, defineString.Find(' ', 8) - 8);
  125. if (originalShaderCode.Find(defineCheck) == String::NPOS)
  126. LOGWARNING("Shader " + GetFullName() + " does not use the define " + defineCheck);
  127. #endif
  128. }
  129. #ifdef RPI
  130. if (type_ == VS)
  131. shaderCode += "#define RPI\n";
  132. #endif
  133. #ifdef EMSCRIPTEN
  134. shaderCode += "#define WEBGL\n";
  135. #endif
  136. if (Graphics::GetGL3Support())
  137. shaderCode += "#define GL3\n";
  138. // When version define found, do not insert it a second time
  139. if (verEnd > 0)
  140. shaderCode += (originalShaderCode.CString() + verEnd);
  141. else
  142. shaderCode += originalShaderCode;
  143. const char* shaderCStr = shaderCode.CString();
  144. glShaderSource(object_, 1, &shaderCStr, 0);
  145. glCompileShader(object_);
  146. int compiled, length;
  147. glGetShaderiv(object_, GL_COMPILE_STATUS, &compiled);
  148. if (!compiled)
  149. {
  150. glGetShaderiv(object_, GL_INFO_LOG_LENGTH, &length);
  151. compilerOutput_.Resize((unsigned)length);
  152. int outLength;
  153. glGetShaderInfoLog(object_, length, &outLength, &compilerOutput_[0]);
  154. glDeleteShader(object_);
  155. object_ = 0;
  156. }
  157. else
  158. compilerOutput_.Clear();
  159. return object_ != 0;
  160. }
  161. void ShaderVariation::SetName(const String& name)
  162. {
  163. name_ = name;
  164. }
  165. void ShaderVariation::SetDefines(const String& defines)
  166. {
  167. defines_ = defines;
  168. }
  169. Shader* ShaderVariation::GetOwner() const
  170. {
  171. return owner_;
  172. }
  173. }