OGLShaderVariation.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // Copyright (c) 2008-2013 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 "Shader.h"
  26. #include "ShaderProgram.h"
  27. #include "ShaderVariation.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. ShaderVariation::ShaderVariation(Shader* owner, ShaderType type) :
  32. GPUObject(owner->GetSubsystem<Graphics>()),
  33. shaderType_(type),
  34. sourceCodeLength_(0),
  35. compiled_(false)
  36. {
  37. }
  38. ShaderVariation::~ShaderVariation()
  39. {
  40. Release();
  41. }
  42. void ShaderVariation::OnDeviceLost()
  43. {
  44. GPUObject::OnDeviceLost();
  45. compiled_ = false;
  46. compilerOutput_.Clear();
  47. if (graphics_)
  48. graphics_->CleanupShaderPrograms();
  49. }
  50. void ShaderVariation::Release()
  51. {
  52. if (object_)
  53. {
  54. if (!graphics_)
  55. return;
  56. if (!graphics_->IsDeviceLost())
  57. {
  58. if (shaderType_ == VS)
  59. {
  60. if (graphics_->GetVertexShader() == this)
  61. graphics_->SetShaders(0, 0);
  62. }
  63. else
  64. {
  65. if (graphics_->GetPixelShader() == this)
  66. graphics_->SetShaders(0, 0);
  67. }
  68. glDeleteShader(object_);
  69. }
  70. object_ = 0;
  71. compiled_ = false;
  72. compilerOutput_.Clear();
  73. graphics_->CleanupShaderPrograms();
  74. }
  75. }
  76. bool ShaderVariation::Create()
  77. {
  78. Release();
  79. if (!sourceCode_ || !sourceCodeLength_)
  80. return false;
  81. object_ = glCreateShader(shaderType_ == VS ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER);
  82. if (!object_)
  83. {
  84. compilerOutput_ = "Could not create shader object";
  85. return false;
  86. }
  87. // Prepend the defines to the shader code
  88. // Check if there is a version definition; it must stay in the beginning
  89. String shaderCode(sourceCode_.Get(), sourceCodeLength_);
  90. String defines;
  91. for (unsigned i = 0; i < defines_.Size(); ++i)
  92. defines += "#define " + defines_[i] + " " + defineValues_[i] + "\n";
  93. if (!defines_.Empty())
  94. defines += "\n";
  95. unsigned pos = 0;
  96. if (shaderCode.StartsWith("#version"))
  97. {
  98. pos = shaderCode.Find('\n');
  99. if (pos != String::NPOS)
  100. ++pos;
  101. else
  102. pos = 0;
  103. }
  104. shaderCode.Insert(pos, defines);
  105. const char* shaderCStr = shaderCode.CString();
  106. glShaderSource(object_, 1, &shaderCStr, 0);
  107. glCompileShader(object_);
  108. int compiled, length;
  109. glGetShaderiv(object_, GL_COMPILE_STATUS, &compiled);
  110. compiled_ = compiled != 0;
  111. if (!compiled_)
  112. {
  113. glGetShaderiv(object_, GL_INFO_LOG_LENGTH, &length);
  114. compilerOutput_.Resize(length);
  115. int outLength;
  116. glGetShaderInfoLog(object_, length, &outLength, &compilerOutput_[0]);
  117. }
  118. else
  119. compilerOutput_.Clear();
  120. return compiled_;
  121. }
  122. void ShaderVariation::SetName(const String& name)
  123. {
  124. name_ = name;
  125. }
  126. void ShaderVariation::SetSourceCode(const SharedArrayPtr<char>& code, unsigned length)
  127. {
  128. sourceCode_ = code;
  129. sourceCodeLength_ = length;
  130. }
  131. void ShaderVariation::SetDefines(const Vector<String>& defines, const Vector<String>& defineValues)
  132. {
  133. if (defines.Size() == defineValues.Size())
  134. {
  135. defines_ = defines;
  136. defineValues_ = defineValues;
  137. }
  138. }
  139. }