OGLShaderVariation.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // Copyright (c) 2008-2014 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.h"
  24. #include "GraphicsImpl.h"
  25. #include "Log.h"
  26. #include "Shader.h"
  27. #include "ShaderProgram.h"
  28. #include "ShaderVariation.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  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. if (graphics_)
  47. graphics_->CleanupShaderPrograms();
  48. }
  49. void ShaderVariation::Release()
  50. {
  51. if (object_)
  52. {
  53. if (!graphics_)
  54. return;
  55. if (!graphics_->IsDeviceLost())
  56. {
  57. if (type_ == VS)
  58. {
  59. if (graphics_->GetVertexShader() == this)
  60. graphics_->SetShaders(0, 0);
  61. }
  62. else
  63. {
  64. if (graphics_->GetPixelShader() == this)
  65. graphics_->SetShaders(0, 0);
  66. }
  67. glDeleteShader(object_);
  68. }
  69. object_ = 0;
  70. graphics_->CleanupShaderPrograms();
  71. }
  72. compilerOutput_.Clear();
  73. }
  74. bool ShaderVariation::Create()
  75. {
  76. Release();
  77. if (!owner_)
  78. {
  79. compilerOutput_ = "Owner shader has expired";
  80. return false;
  81. }
  82. object_ = glCreateShader(type_ == VS ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER);
  83. if (!object_)
  84. {
  85. compilerOutput_ = "Could not create shader object";
  86. return false;
  87. }
  88. const String& originalShaderCode = owner_->GetSourceCode(type_);
  89. String shaderCode;
  90. // Check if the shader code contains a version define
  91. unsigned verStart = originalShaderCode.Find('#');
  92. unsigned verEnd = 0;
  93. if (verStart != String::NPOS)
  94. {
  95. if (originalShaderCode.Substring(verStart + 1, 7) == "version")
  96. {
  97. verEnd = verStart + 9;
  98. while (verEnd < originalShaderCode.Length())
  99. {
  100. if (IsDigit(originalShaderCode[verEnd]))
  101. ++verEnd;
  102. else
  103. break;
  104. }
  105. // If version define found, insert it first
  106. String versionDefine = originalShaderCode.Substring(verStart, verEnd - verStart);
  107. shaderCode += versionDefine + "\n";
  108. }
  109. }
  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. // Prepend the defines to the shader code
  113. Vector<String> defineVec = defines_.Split(' ');
  114. for (unsigned i = 0; i < defineVec.Size(); ++i)
  115. {
  116. // Add extra space for the checking code below
  117. String defineString = "#define " + defineVec[i].Replaced('=', ' ') + " \n";
  118. shaderCode += defineString;
  119. // In debug mode, check that all defines are referenced by the shader code
  120. #ifdef _DEBUG
  121. String defineCheck = defineString.Substring(8, defineString.Find(' ', 8) - 8);
  122. if (originalShaderCode.Find(defineCheck) == String::NPOS)
  123. LOGWARNING("Shader " + GetFullName() + " does not use the define " + defineCheck);
  124. #endif
  125. }
  126. #ifdef RPI
  127. if (type_ == VS)
  128. shaderCode += "#define RPI\n";
  129. #endif
  130. // When version define found, do not insert it a second time
  131. if (verEnd > 0)
  132. shaderCode += (originalShaderCode.CString() + verEnd);
  133. else
  134. shaderCode += originalShaderCode;
  135. const char* shaderCStr = shaderCode.CString();
  136. glShaderSource(object_, 1, &shaderCStr, 0);
  137. glCompileShader(object_);
  138. int compiled, length;
  139. glGetShaderiv(object_, GL_COMPILE_STATUS, &compiled);
  140. if (!compiled)
  141. {
  142. glGetShaderiv(object_, GL_INFO_LOG_LENGTH, &length);
  143. compilerOutput_.Resize(length);
  144. int outLength;
  145. glGetShaderInfoLog(object_, length, &outLength, &compilerOutput_[0]);
  146. glDeleteShader(object_);
  147. object_ = 0;
  148. }
  149. else
  150. compilerOutput_.Clear();
  151. return object_ != 0;
  152. }
  153. void ShaderVariation::SetName(const String& name)
  154. {
  155. name_ = name;
  156. }
  157. void ShaderVariation::SetDefines(const String& defines)
  158. {
  159. defines_ = defines;
  160. }
  161. Shader* ShaderVariation::GetOwner() const
  162. {
  163. return owner_;
  164. }
  165. }