OGLShaderVariation.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. compiled_(false)
  37. {
  38. }
  39. ShaderVariation::~ShaderVariation()
  40. {
  41. Release();
  42. }
  43. void ShaderVariation::OnDeviceLost()
  44. {
  45. GPUObject::OnDeviceLost();
  46. compiled_ = false;
  47. compilerOutput_.Clear();
  48. if (graphics_)
  49. graphics_->CleanupShaderPrograms();
  50. }
  51. void ShaderVariation::Release()
  52. {
  53. if (object_)
  54. {
  55. if (!graphics_)
  56. return;
  57. if (!graphics_->IsDeviceLost())
  58. {
  59. if (type_ == VS)
  60. {
  61. if (graphics_->GetVertexShader() == this)
  62. graphics_->SetShaders(0, 0);
  63. }
  64. else
  65. {
  66. if (graphics_->GetPixelShader() == this)
  67. graphics_->SetShaders(0, 0);
  68. }
  69. glDeleteShader(object_);
  70. }
  71. object_ = 0;
  72. compiled_ = false;
  73. compilerOutput_.Clear();
  74. graphics_->CleanupShaderPrograms();
  75. }
  76. }
  77. bool ShaderVariation::Create()
  78. {
  79. Release();
  80. if (!owner_)
  81. {
  82. compilerOutput_ = "Owner shader has expired";
  83. return false;
  84. }
  85. object_ = glCreateShader(type_ == VS ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER);
  86. if (!object_)
  87. {
  88. compilerOutput_ = "Could not create shader object";
  89. return false;
  90. }
  91. const String& originalShaderCode = owner_->GetSourceCode(type_);
  92. String shaderCode;
  93. // Distinguish between VS and PS compile in case the shader code wants to include/omit different things
  94. shaderCode += type_ == VS ? "#define COMPILEVS\n" : "#define COMPILEPS\n";
  95. // Prepend the defines to the shader code
  96. Vector<String> defineVec = defines_.Split(' ');
  97. for (unsigned i = 0; i < defineVec.Size(); ++i)
  98. {
  99. // Add extra space for the checking code below
  100. String defineString = "#define " + defineVec[i].Replaced('=', ' ') + " \n";
  101. shaderCode += defineString;
  102. // In debug mode, check that all defines are referenced by the shader code
  103. #ifdef _DEBUG
  104. String defineCheck = defineString.Substring(8, defineString.Find(' ', 8) - 8);
  105. if (originalShaderCode.Find(defineCheck) == String::NPOS)
  106. LOGWARNING("Shader " + GetName() + " does not use the define " + defineCheck);
  107. #endif
  108. }
  109. #ifdef RASPI
  110. if (type_ == VS)
  111. shaderCode += "#define RASPI\n";
  112. #endif
  113. shaderCode += originalShaderCode;
  114. const char* shaderCStr = shaderCode.CString();
  115. glShaderSource(object_, 1, &shaderCStr, 0);
  116. glCompileShader(object_);
  117. int compiled, length;
  118. glGetShaderiv(object_, GL_COMPILE_STATUS, &compiled);
  119. compiled_ = compiled != 0;
  120. if (!compiled_)
  121. {
  122. glGetShaderiv(object_, GL_INFO_LOG_LENGTH, &length);
  123. compilerOutput_.Resize(length);
  124. int outLength;
  125. glGetShaderInfoLog(object_, length, &outLength, &compilerOutput_[0]);
  126. }
  127. else
  128. compilerOutput_.Clear();
  129. return compiled_;
  130. }
  131. void ShaderVariation::SetName(const String& name)
  132. {
  133. name_ = name.Trimmed().Replaced(' ', '_');
  134. }
  135. void ShaderVariation::SetDefines(const String& defines)
  136. {
  137. defines_ = defines;
  138. }
  139. }