OGLShaderVariation.cpp 6.5 KB

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